Jump to content

Rename Images


Pandareen

Recommended Posts

Ok, so i've managed to upload images, but now i'm trying to rename them to avoid replacement and i've done something like this:

 

mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`)

VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');"); 

 

  
function findexts ($filename) { 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1; $exts = $exts[$n]; 
return $exts; } 


$ext = findexts ($_FILES['foto']['name']) ;   
$ran2 = $foto.".";  
$target = "../images/";

$target = $target . $ran2.$ext; 
while (file_exists($target)) { 
    $ran2 = 'copyof'.$ran2; 
    $target = $target . $ran2.$ext; 
} 
if(move_uploaded_file($_FILES['foto']['tmp_name'], $target))  { 
echo "The file has been uploaded as ".$ran2.$ext; } 

else { echo "Sorry, there was a problem uploading your file."; } 



header('Location: succes.php');

 

but it renames them in a wierd way...what is wrong?

Link to comment
Share on other sites

First can u tell us that wat is $foto variable holding i want to know what does it holds

 

 

And i you don't mind can i suggest u that why don't u change the name of the file on every upload means give new name to the file whenever user uploads a file use date function like this

 

$filename = date('dmYHis');

 

once u got he file name append the extension and store it in the folder, using this u will get a unique name for every upload that u do

Link to comment
Share on other sites

well $foto is my picture ( poza ) and in my data base is BLOB type...is this correct?

 

mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) 
VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');");  

  
function findexts ($filename) { 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1; $exts = $exts[$n]; 
return $exts; } 


$ext = findexts ($_FILES['foto']['name']) ;   
$ran2 = $foto.".";  
$target = "../images/";

$target = $target . $ran2.$ext; 
while (file_exists($target)) { 
    $ran2 = 'copyof'.$ran2; 
    $target = $target . $ran2.$ext; 
} 
if(move_uploaded_file($_FILES['foto']['tmp_name'], $target))  { 
																echo "The file has been uploaded as ".$ran2.$ext; } 

else { echo "Sorry, there was a problem uploading your file."; } 


Link to comment
Share on other sites

If i m not mistaken i think the problem is that the name that u r storing in database is different and and file name is different because u r executing insert statement before the move_upload statement

 

Execute insert statement after the upload statement and instead of using $foto us $ran2

Link to comment
Share on other sites

Ok!So i have a table users: userID, name, surname, photo and with a html form i insert data in that table.It works fine with the first row...but when i want to upload another picture, it replaces the first one, and this is my problem....i want to upload images with diferent names and i don't know how to fix that code:

 

mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`) 
VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$foto', '$_POST[pret]', '$_POST[disponibilitate]');");  

  
function findexts ($filename) { 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1; $exts = $exts[$n]; 
return $exts; } 


$ext = findexts ($_FILES['foto']['name']) ;   
$ran2 = $foto.".";  
$target = "../images/";

$target = $target . $ran2.$ext; 
while (file_exists($target)) { 
    $ran2 = 'copyof'.$ran2; 
    $target = $target . $ran2.$ext; 
} 
if(move_uploaded_file($_FILES['foto']['tmp_name'], $target))  { 
echo "The file has been uploaded as ".$ran2.$ext; } 

else { echo "Sorry, there was a problem uploading your file."; } 



header('Location: succes.php');

 

so, with this i take the image (foto) and i move it in "images" (EX: 1.jpg ) and now if i upload another one, the scrit saves it with a wierd name like 1.jpgcopy1.jpg and it can't be echo...

 

sorry for my english  :-s and yes i know that i am a noob :(

Link to comment
Share on other sites

Can you try the code the way i have written don't change it place it has it is and try it out

 

function findexts ($filename) {

$filename = strtolower($filename) ;

$exts = split("[/\\.]", $filename) ;

$n = count($exts)-1; $exts = $exts[$n];

return $exts; }

 

 

$ext = findexts ($_FILES['foto']['name']) ; 

$ran2 = $foto."."; 

$target = "../images/";

 

$target = $target . $ran2.$ext;

while (file_exists($target)) {

    $ran2 = 'copyof'.$ran2;

    $target = $target . $ran2.$ext;

}

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

$newFilename = $ran2 . "." . $ext;

mysql_query( "INSERT INTO `softmarket`.`internet_securitate` (`internet_securitateID`, `nume_produs`, `descriere`, `versiune`,`poza`, `pret`, `disponibilitate_produs`)

VALUES ('', '$_POST[titlu]', '$_POST[descriere]', '$_POST[versiune]', '$newFilename', '$_POST[pret]', '$_POST[disponibilitate]');");

echo "The file has been uploaded as ".$newFilename; }

 

else { echo "Sorry, there was a problem uploading your file."; }

 

 

 

header('Location: succes.php');

Link to comment
Share on other sites

Are you trying to increment a number on the end of the file if there's a matching name? So original is "foo.jpg", next uploaded with the same name would be "foocopy1.jpg", next "foocopy2.jpg", etc. If so your code is all over the place, and you're inserting the file name into the database before actually knowing what it will be called.

 

Personally I would just append a timestamp and pass the filename through md5(), as opposed to keep incrementing a number on the end. If you would rather do it this way though, I've quickly thrown together this function which should do the trick. Please read the comments and analyse the code (researching online if needed) before asking questions on something.

 

function getNthFilename($filename, $target_dir)
{
// Make sure the filename actually does already exists
if (!file_exists($target_dir . $filename)) {
	return $filename;
}

// Define the copy filename text
$copy_text = $filename . 'copy%d';

// Define the number to increment
$copy_num = 1;

// Define the first copied version of the filename to test
$copied_filename = sprintf($copy_text, $copy_num);

// Loop through until we find an available name
while (file_exists($target_dir . $copy_file)) {
	$copied_filename = sprintf($copy_text, ++$copy_num);
}

// At this point we should have a valid name
return $copied_filename;
}

 

You can use it like:

 

// Get the filename
$filename = $_FILES['foto']['name'];

// Get an absolute path for the target directory
$target = realpath('../images') . '/';

// Get the valid name for the file
$valid_filename = getNthFilename($filename, $target);

 

After then successfully uploading the image, and only then, you want to insert the $valid_filename into the database.

 

Edit

 

I've corrected a quick typo. Might be more, I haven't tested this..

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.