Jump to content

uploading & storing image problem


phpwannabe25

Recommended Posts

Hello!

 

I am uploading photos to an images folder in my database, and savin the image name in a table.

In order to prevent from duplicating the name of the image, i have included a random image name function, which assigns each image with a new randomly generated number.

 

This is working and is storing the new name in the table perfectly.

 

however, the problem is that the image that is uploaded to the images folder, still has the origianl name and is not then recognized when i try and call the image to the page..

 

the code so far is.....

any suggestions are appreciated.

 

<?php 
//This function separates the extension from the rest of the file name and returns it 
function findexts ($filename) 
{ 
$filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; 
$exts = $exts[$n]; 
return $exts; 
} 

//This applies the function to our file 
$ext = findexts ($_FILES['uploaded']['name']) ; 

//This line assigns a random number to a variable. 
$ran = rand () ;

//This takes the random number  you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 

// Connects to your Database
mysql_connect("xxx", "yyy", "zzz") or die(mysql_error()) ;
mysql_select_db("sssss") or die(mysql_error()) ;

//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2.$ext')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded as ".$ran2.$ext;
} 

else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

Link to comment
Share on other sites

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

 

is the same as:

 

$ext = pathinfo($filename, PATHINFO_EXTENSION);

 

it's not $target . basename($_FILES[..]) but:

 

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

 

mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2.$ext')") ;

 

$ran2.$ext is wrong because it will insert filename..extension

 

P.S. $pic=($_FILES['photo']['name']); $pic is never used

Link to comment
Share on other sites

thanks for your help!

 

I have made the changes there and now i have it workin so that the file is renamed, it is stored in the images folder and the table with the new name. And i have tested it by adding a different image with the same name and both images are renamed and recalled back correctly.

 

my only concern is now, is that the file is not being saved with an extension, or any extension at all

it still works, however it would be good to get the extension saved also.

 

<?php 
//This function separates the extension from the rest of the file name and returns it 
$ext = pathinfo($filename, PATHINFO_EXTENSION);

//This applies the function to our file 
$ext = findexts ($_FILES['uploaded']['name']) ; 

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer. 
$ran = rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . $ran2 . $ext;

//This gets all the other information from the form 
$name=$_POST['name']; 
$email=$_POST['email']; 
$phone=$_POST['phone']; 
$pic=($_FILES['photo']['name']); 

// Connects to your Database
mysql_connect("xxxx", "zzzz", "yyyy") or die(mysql_error()) ;
mysql_select_db("hhhhhl") or die(mysql_error()) ;

//Writes the information to the database 
mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$ran2')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded as ".$ran2.$ext;
} 

else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 
?> 

Link to comment
Share on other sites

//This function separates the extension from the rest of the file name and returns it 
$ext = pathinfo($filename, PATHINFO_EXTENSION);

//This applies the function to our file 
$ext = findexts ($_FILES['uploaded']['name']) ; 

 

should be:

 

//This function separates the extension from the rest of the file name and returns it 
$ext = pathinfo($_FILES['uploaded']['name'], PATHINFO_EXTENSION);

Link to comment
Share on other sites

thanks very much for your help,

much appreciated!

 

i am tryin to integrate that php code in to a longer signup document.

 

the code i am using at the  moment, says 'your image has been saved as....etc'

	//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file has been uploaded as ".$ran2.$ext;
} 

else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
}

 

however, what i am hopin for , is that the only time a message will appear is that there has been a problem,

and otherwsie the script will run..

 

so basically i am tryin to get it as

somthign like

 

	//Writes the photo to the server 
if!(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 

{ 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
}

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.