phpcoder2013 Posted August 22, 2013 Share Posted August 22, 2013 I need to modify the script to additionally do the following: Once the image is on the server, I want it to duplicate itself, then, rename the new duplicated file to a variable name. (Let's say $TEST . '.jpg' where $TEST = TESTING for this example) My current piece of code: <?php//Сheck that we have a fileif((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350000Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__) . '/Uploads/' . date('Y_m_d-H:i:s') . '-' . $filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Error: Only .jpg images under 350000Kb are accepted for upload"; }} else { echo "Error: No file uploaded";}?> This following piece of code might assist you: <?php$file = 'one.jpg';$newfile = 'one_thumb.jpg'; if (!copy($file, $newfile)) { echo "failed to copy $file...\n";}?> PS: I am quite new to PHP. (Introduced to it only about 3 days ago) Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 22, 2013 Share Posted August 22, 2013 <?php $file = 'one.jpg'; $newfile = 'one_thumb.jpg'; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } ?> What happens here? Quote Link to comment Share on other sites More sharing options...
phpcoder2013 Posted August 22, 2013 Author Share Posted August 22, 2013 <?php $file = 'one.jpg'; $newfile = 'one_thumb.jpg'; if (!copy($file, $newfile)) { echo "failed to copy $file...\n"; } ?> What happens here? I think it copies from one location to another then renames it. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 23, 2013 Share Posted August 23, 2013 (edited) I think it copies from one location to another then renames it. Then, we will have a big mess PS: Rename the old file with rename php function, then copy this new file to get a duplicate name to other directory. Edited August 23, 2013 by jazzman1 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted August 23, 2013 Share Posted August 23, 2013 Does somebody try to move uploaded temp file to two different directories on the server. Or...when this function moved the temp file, the file has been deleted by php forever. Never tested. Something like: $tmp_name = $_FILES["pictures"]["tmp_name"] move_uploaded_file($tmp_name, "$uploads_dir1/$name"); move_uploaded_file($tmp_name, "$uploads_dir2/$name"); Quote Link to comment Share on other sites More sharing options...
phpcoder2013 Posted August 23, 2013 Author Share Posted August 23, 2013 Then, we will have a big mess PS: Rename the old file with rename php function, then copy this new file to get a duplicate name to other directory. Just tested the code and worked as expected. Copied example.txt to example.txt.bak in the same directory. Can you tell me why I will have a big mess? Quote Link to comment 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.