php - How to rename each file before uploading to server to a time-stamp -
I am using the following code to upload some files, but alright, some names change because the name Are the same. I just want to know how to change the name, I tried, but I tried to mess around myself with the whole code.
PHP I have to do something with this variable generated from time to time in every file. Thanks. I do not want to check first whether the file exists as it adds to the processes, I just need time () and some random string. Just to get a unique name. Please refer to uploading the file and for a good example of an explanation. It also appears from where you got this particular script or if it does not provide a good explanation. I have modified your code to include some comments so that you and others can understand what is going on better. In theory, the code should not overwrite existing files like you say it but I have added that you will need to change the name of the file to be set in random string. Additionally you store the original file name in the session and not the modified file name. Read more about PHP. I would also strongly advise to check some file types because currently it appears that any file may be uploaded, the second link above is a section called 'Security Issues' which I Carefully read and recommend separately.
if (empty ($ _ files ["album_art"])) {$ myFile = $ _FILES ["album_art" ]; If ($ myFile ["error"]! == UPLOAD_ERR_OK) {resonant "& lt; p> an error occurred. Go out; } $ Name = preg_replace ("/ [^ A-Z0-9 ._-] / i", "_", $ myFile ["name"]); $ I = 0; $ Parts = pathinfo ($ name); While (file_exists (name UPLOAD_DIR. $)) {$ I ++; $ Name = $ parts ["filename"] "-" $ I. "." . $ Parts ["extension"]; } $ Success = step_upload_file ($ myFile ["tmp_name"], UPLOAD_DIR. '/'. $ Name); If (! $ Success) {resonance "& lt; p & gt; unable to save file. & Lt; / p & gt;"; Go out; } And {$ Dir = UPLOAD_DIR. '/' $ _FILES ["ambum_art"] ["name"]; } Chmod (UPLOAD_DIR. '/'. $ Name, 0644); Not set ($ _ session ['video']); $ _SESSION ['album_art'] = $ Dir; $ Ambum_art_result = array (); $ Ambum_art_result ['content'] = $ Dir; Echo json_encode ($ ambum_art_result); }
$ rand_name = microtime (). Microtime () Time () .microtime ();
Define ("UPLOAD_DIR", "/ path / to / uploads /"); If (! Empty ($ _ files ["anbum_art"])) {// file $ myFile = $ _FILES ["album_art"] uploaded; // Check that if ($ myFile ["Error"]! == UPLOAD_ERR_OK) was no error {resonant "& lt; p> an error occurred. "; Go out; } // Rename the file so that only A-Z, 0-9 get _ - $ name = preg_replace ("/ [^ A-Z0-9 ._-] / i", "_", $ myFile ["name "]); // Split the parts into useful parts $ parts = pathino ($ name); // This part of the code should continue until the loop until a file name that does not already exist. $ I = 0; While (file_exists (name UPLOAD_DIR. $)) {$ I ++; $ Name = $ parts ["filename"] "-" $ I. "." . $ Parts ["extension"]; } // If you want to set a random unique name for the file, remove the following line and remove the above // $ name = uniqid (). $ Parts ["extension"]; // Now save the uploaded file to new name in your upload directory $ success = move_uploaded_file ($ myFile ["tmp_name"], UPLOAD_DIR. '/'. $ Name); // If the savings fail then if the execution survives (if! $ Success) {resonant "& lt; p & gt; unable to save the file. "; Go out; } $ // Dir Dir $ Dir set the file path to set = UPLOAD_DIR '/' $ Name; // Set permissions on the newly uploaded file chmod ($ Dir, 0644); // Your app specific session stuff unset ($ _ session ['video']); $ _SESSION ['album_art'] = $ Dir; Save the file path to // session $ ambum_art_result = array (); $ Ambum_art_result ['content'] = $ Dir; Echo json_encode ($ ambum_art_result); }
Comments
Post a Comment