swamp Posted August 20, 2008 Share Posted August 20, 2008 Ok, heres my script for uploading an image and creating a text file with a short biog for each author: <?php $file = "../../author/".$_POST['poster'].".txt"; $content = stripslashes($_POST['author-biog']); $fp = fopen($file,"w") or die ("Error opening file in write mode!"); fputs($fp,$content); fclose($fp) or die ("Error closing file!"); ?> <? $target_path = "../../../images/straight-talk/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " is uploading... please wait..."; echo "<meta http-equiv=\"refresh\" content=\"0; url=../admin/?action=newpost\" />\n"; } else{ echo "There was an error uploading the file, please try again!"; echo "<meta http-equiv=\"refresh\" content=\"0; url=../admin/?action=newpost\" />\n"; } ?> I'm sure it is simple to do, but how can I rename the uploaded image file to the same name as $_POST['poster'] ? Cheers Link to comment https://forums.phpfreaks.com/topic/120545-rename-image-on-upload/ Share on other sites More sharing options...
s1yman Posted August 20, 2008 Share Posted August 20, 2008 first, declare $_post['poster'] as its own variable... Link to comment https://forums.phpfreaks.com/topic/120545-rename-image-on-upload/#findComment-621174 Share on other sites More sharing options...
swamp Posted August 20, 2008 Author Share Posted August 20, 2008 yeah so $post = $_POST['poster'] Link to comment https://forums.phpfreaks.com/topic/120545-rename-image-on-upload/#findComment-621178 Share on other sites More sharing options...
s1yman Posted August 20, 2008 Share Posted August 20, 2008 <?php $target = "directory to be uploaded"; function findexts ($filename) { $filename = strtolower($filename) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['poster']['name']) ; $post = $_POST['poster']; $target = $target . $post.$ext; if(move_uploaded_file($_FILES['poster']['tmp_name'], $target)) { print .$target. 'has been uploaded'; } ?> Link to comment https://forums.phpfreaks.com/topic/120545-rename-image-on-upload/#findComment-621183 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.