Jump to content

rename uploaded file how?


$php_mysql$

Recommended Posts

anyone can show example how i could remane the uploaded file name with users id along with some number?

 

//////////////////////////////////////uploader
else if($action=="uploader")
{



echo "Upload your picture and copy the link <br/>after uploading to user it at gallery.<br/><br/>";

echo "<form method=\"post\" enctype=\"multipart/form-data\" action=\"index.php?action=uploaded&sid=$sid\">";
echo "Choose Picture: <br />";
echo "<input name=\"uploaded\" type=\"file\" /><br /><br />";
echo "<input type=\"submit\" value=\"Upload\" />";
echo "</form><br/>";


echo "<p align=\"center\">";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}


//////////////////////////////////////uploader
else if($action=="uploaded")
{



$blacklist = array(".php", ".php.jpg", ".php.jpeg", ".php.gif", ".php.png", ".phtml", ".php3", ".php4");
foreach ($blacklist as $item) {
if(preg_match("/$item\$/i", $_FILES['uploaded']['name'])) {
echo "<p align=\"center\">";
echo "Oops sorry we do not allow those files.<br/>";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>"; 
exit;
}
} 

$target = "../images/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;

if (file_exists("../images/" . $_FILES["uploaded"]["name"]))
      {
echo "<p align=\"center\">";
      echo $_FILES["file"]["name"] . "Oops file name already exists<br/> kindly rename your picture and upload again. <br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";    
}else{

//This is our size condition
if ($uploaded_size > 25600){

echo "Your file is too large. We have a 25kb limit.<br/>";
$ok=0;
}

$types = array('image/jpeg', 'image/gif', 'image/png');

if (in_array($_FILES['uploaded']['type'], $types)) {
// file is okay continue
} else {
$ok=0;
} 


//Here we check that $ok was not set to 0 by an error
if ($ok==0){
echo "<p align=\"center\">";
Echo "Sorry your file was not uploaded.<br/> It may be the wrong filetype. <br/>We only allow JPG, GIF, and PNG filetypes.<br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}

//If everything is ok we try to upload it
else{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "<p align=\"center\">";

echo "The file ". basename( $_FILES['uploadedfile']['name']). " Picture uploaded successfully.<br/><br/><b>$target <br/>";

echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}
else{
echo "<p align=\"center\">";

echo "Sorry, there was a problem uploading your file.<br/>";
echo "<br/><a href=\"http://index.php?action=main&sid=$sid\">Wml Home</a><br />";
echo "<a href=\"index.php?action=main&sid=$sid\">Home</a>";
echo "</p>";
}
}
}
}
[code]

Link to comment
Share on other sites

On this line of your code:

 

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){

 

..you specify the $target location to move the file; modify $target to contain the file name you want.

 

Currently you have:

 

$target = "../images/";
$target = $target . basename( $_FILES['uploaded']['name']) ;

 

Replace it to something like:

 

$target = "../images/";
$target = $target . md5(rand() . $_FILES['uploaded']['name']);

Link to comment
Share on other sites

ok the issue is, when i echo $target it prints the path and the renamed image name something like this

 

images/username28374image.jpg

 

but what im looking for is to get only

 

username28374image.jpg

 

without the images/ so that i could store the username28374image.jpg  name into DB for after i store like images/username28374image.jpg im unable to delete the image from the dir.

 

 

I want to get images/username28374image.jpg with $target but aswell i want a way to get only the image name like username28374image.jpg any help please?

Link to comment
Share on other sites

ok it does but not along with the renamed image but the original name of the image, currently to rename i did

 

$target = "images/";

$randomnum=rand(0000,9999);

$target = $target.getnick_username($username).$randomnum;

$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

 

the uploaded images gets stored like username2345_originalimagename.jpg

 

but when im printing the

 

$image_name = basename( $_FILES['uploaded']['name']) ;

 

i get only originalimagename.jpg not along with username and the random number like username2345_originalimagename.jpg which gets uploaded

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.