bugzy Posted April 26, 2012 Share Posted April 26, 2012 My 1st try practice uploading file to the server and it was successful. Now I wonder how can I set a default file name to the file that will be uploaded and overwrite the existing one. I have tried experimenting on the code but I can't get it to work. Here's my simple code.. edit_logo.php <?php <form enctype="multipart/form-data" method="post" action="uploaded_logo.php"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <tr> <td>Choose a Logo to Upload: </td> <td><input name="uploaded_file" type="file" /></td> </tr> <tr> <td> </td> <td><input type="submit" value="Upload File" name="submit>"</td> </tr> </form> ?> uploaded_logo.php <?php if($_FILES['uploaded_file']["type"] == "image/gif") { $target_path = "logo/"; $target_path = $target_path. basename($_FILES['uploaded_file']['name']); if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) { echo "<span class=\"error_validation\">The Logo has been successfully changed!<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } else { echo "<span class=\"error_validation\">There was an error uploading the logo. Pls. try again.<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } } else { echo "<span class=\"error_validation\">Invalid file format. We are only accepting image file. Pls. try again.<br></span>"; echo "<span class=\"error_validation\">Upload Logo Again? <a href=\"edit_logo.php\">Click Here</a><br></span>"; } ?> So what the code suppose to do is.. If I upload an image file as the new logo it should have the default logo name "my_logo.gif", then it will overwrite the existing one... Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/ Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 $target_path = $target_path. basename($_FILES['uploaded_file']['name']); Change this to have the name you desire. Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340832 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 $target_path = $target_path. basename($_FILES['uploaded_file']['name']); Change this to have the name you desire. Thank you it's working now. Though how would I able to get the extension of that file? instead of typing it like the code below "Logo.gif" <?php $target_path = $target_path. basename('Logo.gif'); ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340841 Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 I would take the old name and explode on "." and choose the last value in that array. You don't need basename anymore. Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340850 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 Here's a couple quick snippets. <?php $file = 'file.name.php'; $ext = array_pop(explode('.',$file)); echo $ext; // php // little less memory $ext = substr($file,strrpos($file,'.')+1); echo $ext; // php ?> Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340858 Share on other sites More sharing options...
Drongo_III Posted April 26, 2012 Share Posted April 26, 2012 You can also do something like: $ext = pathinfo($file, PATHINFO_EXTENSION); Where $file is a handle for your uploaded file. Just another option Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340859 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 pathinfo works, but is quite a bit slower than using arrays, and even slower than string functions. I don't think it would make a huge difference beyond the most efficiency-demanding applications though, so use whatever you find cleanest. This post is just for academic purposes. Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340862 Share on other sites More sharing options...
bugzy Posted April 26, 2012 Author Share Posted April 26, 2012 Thanks guys! Solved! Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340864 Share on other sites More sharing options...
Drongo_III Posted April 26, 2012 Share Posted April 26, 2012 Ahh that's good to know. I might try the array method in future pathinfo works, but is quite a bit slower than using arrays, and even slower than string functions. I don't think it would make a huge difference beyond the most efficiency-demanding applications though, so use whatever you find cleanest. This post is just for academic purposes. Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340865 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 I worded that badly. In terms of time to execute pathinfo > explode/array > substr/strings So, string functions are the most efficient way to get the result. Quite accurate too, unless you're dealing with .tar.gz, etc. Whether 'tar' is actually an extension in this case is debated though. Quote Link to comment https://forums.phpfreaks.com/topic/261662-how-to-set-default-name-in-uploaded-file/#findComment-1340873 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.