Jump to content

Rename image on upload


swamp

Recommended Posts

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

<?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';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.