Jump to content

Recommended Posts

Hi,

 

I need some help in saving the filename along with the extension in the database after i upload the file.

 

Current Scenario:

I am able to upload the file successfully and save the form details in the database.

 

Problems:

When file uploaded with same file name it overwrites the old file.

 

How do i give the file a unique name when uploading the file and then saving the form details along with the new file name given?

 

CODE USED TO SAVE THE FILE TO MYSQL AND UPLOAD THE FILE:

 

<?php

 

//This is the directory where images will be saved

$target = "image/";

$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("localhost", "data_admin2", "data25") or die(mysql_error()) ;

mysql_select_db("Resumebank") or die(mysql_error()) ;

 

 

//Writes the photo to the server

move_uploaded_file($_FILES['photo']['tmp_name'], $target);

 

 

//Writes the information to the database

mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ;

echo "The file has been uploaded, and your information has been added to the directory";

?>

 

 

What should I do? Please help!

 

Isn't it just to use the rename function?

http://php.net/manual/en/function.rename.php

 

Just run a rename on the $target after it has been moved.

 

At least worked for me when I did this:

 

$newname='lol.txt';
if($_FILES){
if(move_uploaded_file($_FILES['userfile']['tmp_name'], basename($_FILES['userfile']['name']))){
	echo 'You successfully uploaded the file: '.$_FILES['userfile']['name'];
	if(rename($_FILES['userfile']['name'],$newname)){
		echo ' and renamed it to '.$newname;
	}else{
		echo ' and failed rename it to '.$newname;
	}
	echo '.';
}else{
	echo 'ERROR';
}
}

<?php

//This is the directory where images will be saved
$target = "image/";
$target = $target . md5() . '-' . 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("localhost", "data_admin2", "data25") or die(mysql_error()) ;
mysql_select_db("Resumebank") or die(mysql_error()) ;


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


//Writes the information to the database
mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ;
echo "The file has been uploaded, and your information has been added to the directory";
?> 

 

James.

<?php

//This is the directory where images will be saved
$target = "image/";
$target = $target . md5() . '-' . 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("localhost", "data_admin2", "data25") or die(mysql_error()) ;
mysql_select_db("Resumebank") or die(mysql_error()) ;


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


//Writes the information to the database
mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ;
echo "The file has been uploaded, and your information has been added to the directory";
?> 

 

James.

 

Don't forget, $pic is also using the filename. Currently, the wrong filename and also a filename which could easily conflict with somebody else's.

 

 

Isn't it just to use the rename function?

http://php.net/manual/en/function.rename.php

 

Just run a rename on the $target after it has been moved.

 

At least worked for me when I did this:

 

$newname='lol.txt';
if($_FILES){
if(move_uploaded_file($_FILES['userfile']['tmp_name'], basename($_FILES['userfile']['name']))){
	echo 'You successfully uploaded the file: '.$_FILES['userfile']['name'];
	if(rename($_FILES['userfile']['name'],$newname)){
		echo ' and renamed it to '.$newname;
	}else{
		echo ' and failed rename it to '.$newname;
	}
	echo '.';
}else{
	echo 'ERROR';
}
}

 

Oh I forgot to add that it's just to check if the name exists and if it does give it a new random one, and loop check if it exists till you are sure it does not! o.o

random file name:

$randomfilename='pic'.rand(0,999999).'.jpg';

for example, but you would have to make sure the extension is right, I think you can get that from the $_FILE variable.

Try adding timestamp, because there could only be one.

 

Try this.

 

<?php

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

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

// Connects to your Database
mysql_connect("localhost", "data_admin2", "data25") or die(mysql_error()) ;
mysql_select_db("Resumebank") or die(mysql_error()) ;


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


//Writes the information to the database
mysql_query("INSERT INTO `employee` VALUES ('$name', '$email', '$phone', '$pic')") ;
echo "The file has been uploaded, and your information has been added to the directory";
?>

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.