PHP simple sample code for uploading and deleting images
- If (! Empty ($ _ FILES ["img"] ["name"]) {// extract the file domain content name and determine
- $ Path = "uppic/"; // Upload path
- If (! File_exists ($ path ))
- {
- // Check whether the folder exists. if not, create the folder and grant the highest permission.
- Mkdir ("$ path", 0700 );
- } // END IF
- // File format that can be uploaded
- $ Tp = array ("image/gif", "image/pjpeg", "image/jpeg ");
- // Check whether the uploaded file is of the allowed Upload type
- If (! In_array ($ _ FILES ["img"] ["type"], $ tp ))
- {
- Echo "script" alert ('incorrectly formatted '); history. go (-1); script ";
- Exit;
- } // END IF
- $ Filetype = $ _ FILES ['IMG '] ['type'];
- If ($ filetype = 'image/jpeg '){
- $ Type = '.jpg ';
- }
- If ($ filetype = 'image/jpg '){
- $ Type = '.jpg ';
- }
- If ($ filetype = 'image/pjpeg '){
- $ Type = '.jpg ';
- }
- If ($ filetype = 'image/GIF '){
- $ Type = '.gif ';
- }
- If ($ _ FILES ["img"] ["name"])
- {
- $ Today = date ("YmdHis"); // Obtain the time and assign it to the variable
- $ File2 = $ path. $ today. $ type; // the complete path of the image.
- $ Img = $ today. $ type; // image name
- $ Flag = 1;
- } // END IF
- If ($ flag) $ result = move_uploaded_file ($ _ FILES ["img"] ["tmp_name"], $ file2 );
- // Note: The first parameter passed to move_uploaded_file is the temporary file uploaded to the server.
- } // END IF
- // Write the $ img value to the corresponding field in the database.
2. delete images in php:
- Unlink ("uppic/". $ img); // unlink method to delete an object
>>> Articles you may be interested in: php deleting records and deleting image files code php deleting uploaded images and folders (instance sharing) small example of PHP deleting all files created N minutes ago php deleting directories and all files methods example of php deleting all files under the Directory code of N days ago php deleting records while refreshing the current page php code for deleting all files in a specified folder simple example for uploading and deleting images php writes a function for deleting directories php recursively creates and deletes folders php recursively deletes directories and file user-defined function rrmdirphp recursively delete a directory example |