Jump to content

Upload file to mySQL - change filename


Recommended Posts

Hi all,

 

I have a form where a user inputs several values and then uploads a file. I'd like to change that file to either a random value or perhaps the ID field so that the files don't get overwritten. Apologies for posting a redundant question (I noticed this has already been asked) but I couldn't get their solution to work on my page and need to see how it would work on my code. Thanks in advance! Pictures of puppies/jokes/etc. for anyone who can help :)

 

Code for the PHP piece...

 

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; 


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

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

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

Link to comment
Share on other sites

Oh I see. For starters, I wouldn't be putting it into the database until it's been uploaded.. Try this code:

 

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Check if file exists
if(file_exists($target.$_FILES['filename']['name'])){
//Yup,it exists. Lets add a bit more to the name...
$split = explode(".", $_FILES['filename']['name']);
$uploadName = $split[0].(md5(rand(1,999)*microtime()).".".$split[1]; //this is assuming you have no other . in your filenames.
}else{
$uploadName = $_FILES['filename']['name'];
}

//Writes the photo to the server 
if(move_uploaded_file($uploadName, $target)) 
{ 

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

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

 

Hope that works. :)

Link to comment
Share on other sites

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; 

// Get last insert ID
$lastid = mysql_insert_id();

//Writes the photo to the server 
if(move_uploaded_file($_FILES['filename']['tmp_name'], $target . $lastid . "_" . basename($_FILES['filename']['name']) )) 
{ 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

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

 

 

Roughly something like that.  (Untested!)  - that should put the insert ID in front of the filename.

Link to comment
Share on other sites

My bad.. Here is mine again:

 

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Check if file exists
if(file_exists($target.$_FILES['filename']['name'])){
//Yup,it exists. Lets add a bit more to the name...
$split = explode(".", $_FILES['filename']['name']);
$uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames.
}else{
$uploadName = $_FILES['filename']['name'];
}

//Writes the photo to the server 
if(move_uploaded_file($uploadName, $target)) 
{ 

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$filename')") ; 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

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

Link to comment
Share on other sites

Hi JonnyThunder,

 

When I use your code, the following happens:

 

If I upload a file called TREES.JPG, it goes into the folder TREES.JPG24_TREES.JPG (I've already tested 23 files...)

 

Any chance you can help me re-name it do there's only one name + extension? (e.g. 24_TREES.JPG)

 

Also, the file is correctly named in the folder, but the $_POST call is still inserting the original filename. Any help making it so the database update and the folder both have identical re-named files would be a huge help.

 

THANKS SO MUCH!!

 

 

 

 

 

Link to comment
Share on other sites

Ha. Man am I on fire. :P

 

Okay try it like this, otherwise... I'm going to bed.

 

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Check if file exists
if(file_exists($target.$_FILES['filename']['tmp_name'])){
//Yup,it exists. Lets add a bit more to the name...
$split = explode(".", $_FILES['filename']['tmp_name']);
$uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames.
}else{
$uploadName = $_FILES['filename']['tmp_name'];
}

//Writes the photo to the server 
if(move_uploaded_file($uploadName, $target)) 
{ 

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$uploadName')") ; 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

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

Link to comment
Share on other sites

Any other ideas? Currently, we have one submission that renames the file going into the folder, but not the data going into the database. We have another submission that renames the data going into the database but not the file going into the folder.

 

...so close and yet so far.

 

Any other ideas are most welcome.

 

Thanks in advance!

Link to comment
Share on other sites

Hi everyone,

 

Just to let you know that I think I've figured it out. Thanks to ProjectFear and JonnyThunder - I used both of your examples as inspiration. In the end, I went for a random generator instead of incremental IDs, since it felt a bit more safe in a race situation. Here's the code I ended up with. It appears to work perfectly.

 

<?php 

//This is the directory where images will be saved 
$target = "uploads/"; 


//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 


// Get last insert ID
$lastid = md5(uniqid()) . "_" . basename($_FILES['filename']['name']);


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

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), 

'$lastid')") ; 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has 

been added to the directory"; 
} 
else { 

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

Link to comment
Share on other sites

Haha, I think thats where I went wrong. This would be my code, fixed. I hope:

 

<?php 

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

//This gets all the other information from the form 
$name=$_POST['name']; 
$description=$_POST['description']; 
$filename=$_POST['filename'];
$filename=($_FILES['filename']['name']); 


// Connects to your Database 
mysql_connect("XXX", "XXX", "XXX") or die(mysql_error()) ; 
mysql_select_db("XXX") or die(mysql_error()) ; 

//Check if file exists
if(file_exists($target.$_FILES['filename']['tmp_name'])){
//Yup,it exists. Lets add a bit more to the name...
$split = explode(".", $_FILES['filename']['tmp_name']);
$uploadName = $split[0].(md5(rand(1,999)*microtime())).".".$split[1]; //this is assuming you have no other . in your filenames.
}else{
$uploadName = $_FILES['filename']['tmp_name'];
}

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

//Writes the information to the database 
mysql_query("INSERT INTO voices(name,description,uploaddate,filename) VALUES ('$name', '$description', NOW(), '$uploadName')") ; 

//Tells you if its all ok 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//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.