co.ador Posted July 27, 2011 Share Posted July 27, 2011 The Global $_FILE field value is <?php basename($_FILES['file']['name'] ?> the variable value is <?php $newname = $id . ".jpg" ?> The Field "file" is carry a value of a picture for instance penguin.jpg and then I want to replace that name for $id . ".jpg" to make a new name for the file and replace it for the id fo the product like 34.jpg. I am working on an upload file script so far is moving the file but with the original name and I trying to make a new and rename it to the id of the product the script is as below and so far is inserting the original name of the file in $uploaddir . basename($_FILES['file']['name'] instead I am looking for something like $uuploaddir . $newname which #newname is equal to $id. "jpg" <?php // $newname = $id . ".jpg"; $uploaddir = '/home3/ny/public_html/your/inventory_images/'; $uploadfile = $uploaddir . basename($_FILES['file']['name']); if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "File was not uploaded\n"; } ?> Any suggestions? so far as I said the existing code is uploading but with the original name file. Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/ Share on other sites More sharing options...
marcus Posted July 27, 2011 Share Posted July 27, 2011 Where to find out which ID it will be? move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir . $id . ".jpg"); Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/#findComment-1247968 Share on other sites More sharing options...
co.ador Posted July 27, 2011 Author Share Posted July 27, 2011 mga I just put a snipet of the whole code but the $id is all the way in the code and it is echoing. I will try your suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/#findComment-1247975 Share on other sites More sharing options...
marcus Posted July 27, 2011 Share Posted July 27, 2011 You can rename the file to however you please really, with exception. But you don't have to use any of the uploaded file name as your renamed file. Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/#findComment-1247977 Share on other sites More sharing options...
co.ador Posted July 27, 2011 Author Share Posted July 27, 2011 @MGA ok, i can see how you did directly. now, When i submitted the form it print "File is valid, and was successfully uploaded" but when I go to the inventory_images I don't find neither the orginal file name or the renamed one which would be 194.jpg which was the id created this time. I have the script set up to echo the $id variale after form has submitted but I don't see the file. Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/#findComment-1247990 Share on other sites More sharing options...
co.ador Posted July 27, 2011 Author Share Posted July 27, 2011 MGA never mind it was inserting all this time even before the thing is I was using $id = SESSION['user_id']; and I needed $product_id = $_POST['product_id']; Sorry for the inconvenience. Thank you for your post. Quote Link to comment https://forums.phpfreaks.com/topic/242973-how-to-replace-a-_file-global-name-for-a-variable-value/#findComment-1247997 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.