Change or insert a picture in a folder with PHP

I created a website for a florist to promote her floral composition.

                For that, I had to create an administration system for replace the pictures that are in folder.

                Because the pictures weren’t stocked in the Data Base, I was in the obligation to change picture stocked in folder. The system can also be used for a profile picture system.

                The administrator has to choose his picture from a folder of his PC. To navigate in the folders of the PC, we use in a form the ‘<input type=’files’> ‘, that look like that :

Input type files

                The files selected his conserved as a ‘$_FILES’, a type of variable that have multiple property like his name, his localization in the PC, the possible error and his size in bits.

                The website can only show pictures. We need to check the file extension so, if the file selected isn’t a type that is authorized, an error appears and abort the program.  For checking the file extension, we use a table that list all the authorize extension. If the last character of the file localization is the same as one of the authorized extension, that mean that if it’s a picture or a gif, the program continue.

                The website is structure so the files is place with his name. If a picture has another name that the one enter in the ‘<img>’, the picture will not display. So, we need to rename the file so we don’t have to do it manually every time.

                We do that with ‘move_upload_files() ‘ that will insert the picture in the file.

‘ move_upload_files() ‘ need 2 parameters : the path of the uploaded file and the path of where it’s going to be placed (we selected that previously with a dropdown list). In the path, we provide the new name of the file so it will be replacing. Here, we replace the base name of the picture with the one we have selected: ‘Composition_Floral_*name of the page*_Au_Grain_de_Pollen_*number*.JPG’

[‘fichier’][‘tmp_name’] : the temporary and default path of the file uploaded
[‘choix_image’] : the new path with the new name of the file

                The file is now place in the right folder, replacing the old file if there was one in there before.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *