Vinlock Posted May 20, 2012 Share Posted May 20, 2012 Hello, I've looked everywhere for help, is there any chance anyone here that knows of PHP can help me out? I have this snippet of code for uploading files. I really want it to rename the files with a date and/or time or anything random at the end of the image's name as it uploads. Would be an amazing help!! Thanks! Here is the code. <?php // Set the uplaod directory $uploadDir = '/images/'; if (!empty($_FILES)) { $tempFile = $_FILES['Filedata']['tmp_name'][0]; $uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir; $targetFile = $uploadDir . $_FILES['Filedata']['name'][0]; // Validate the file type $fileTypes = array('jpg'); // Allowed file extensions $fileParts = pathinfo($_FILES['Filedata']['name'][0]); // Validate the filetype if (in_array($fileParts['extension'], $fileTypes)) { // Save the file move_uploaded_file($tempFile,$targetFile); echo 1; } else { // The file type wasn't allowed echo 'Invalid file type.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262803-trying-to-get-this-php-upload-code-to-rename-the-file/ Share on other sites More sharing options...
requinix Posted May 20, 2012 Share Posted May 20, 2012 $targetdir is what you need to modify. Keep in mind that $uploadDir is just "/images/" and the $_FILES bit is just a filename (with an extension). Quote Link to comment https://forums.phpfreaks.com/topic/262803-trying-to-get-this-php-upload-code-to-rename-the-file/#findComment-1346947 Share on other sites More sharing options...
Vinlock Posted May 20, 2012 Author Share Posted May 20, 2012 there is no $targetdir o.O Quote Link to comment https://forums.phpfreaks.com/topic/262803-trying-to-get-this-php-upload-code-to-rename-the-file/#findComment-1346995 Share on other sites More sharing options...
givememore Posted May 20, 2012 Share Posted May 20, 2012 Vinlock ment $targetFile, I guess. If you want to alter the filename (witout extension) you first have to devide the filename from extension. For example with: $FileNameComplete = explode('.', $_FILES['Filedata']['name'][0]); $FileName = $FileNameComplete[count($FileNameComplete)-1]; $FileExt = $FileNameComplete[count($FileNameComplete)]; Then you can puzzle the new Filename you want the uploaded file to have, i.e. $targetFile = $uploadDir . $FileName . "_" .date('U') . "." . $FileExt; Quote Link to comment https://forums.phpfreaks.com/topic/262803-trying-to-get-this-php-upload-code-to-rename-the-file/#findComment-1347013 Share on other sites More sharing options...
requinix Posted May 20, 2012 Share Posted May 20, 2012 Vinlock ment $targetFile, I guess. Uh yeah, I did too. $uploadDir + $targetFile = $targetDir, right? Quote Link to comment https://forums.phpfreaks.com/topic/262803-trying-to-get-this-php-upload-code-to-rename-the-file/#findComment-1347042 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.