Page 1 of 1
how do rename a file and insert it's location into a databas
Posted: Fri Oct 10, 2003 6:25 pm
by belovedone
I've figured out how to upload an image (thank goodness) but now I want that same form that uploads the file to rename the image file to a unique name as well as input into a row in a database the location of the image file. To be more specific, I've got a whole form with like 5 fields for a user to input info and then the last field the user inputs the location of a file they will be uploading from their computer. Then when the user hit's "submit" the form would need to upload the file, rename it to a unique name, and then input all the information from that form into a database including the location of the image just uploaded.
Posted: Fri Oct 10, 2003 11:03 pm
by DuFF
I have done all of this in a recent script I made. It allows you to even check the dimensions and you can add or take out anything you want. Here it is:
Code: Select all
<?php
$newimname="whateveryouwant"; //name for image after uploading
$uploaddir="path/to/any/folder"; //upload folder
$imtype=$_FILES["imgfile"]["type"];
$imsize=$_FILES["imgfile"]["size"];
$imname=$_FILES["imgfile"]["name"];
$imtemp=$_FILES['imgfile']['tmp_name'];
$immaxwidth="640";
$immaxheight="480";
if (!$error_message){
$imgsize = GetImageSize($imgfile);
//== check size 0=width, 1=height
if (($imgsize[0] > $immaxwidth) || ($imgsize[1] > $immaxheight)){
$error_message="Image upload failed. Please edit your image to be $immaxwidth x $immaxheight px.";
}
}// end if error_message
if (!$error_message){
if (($imtype == "image/pjpeg") || ($imtype == "image/jpeg") || ($imtype == "image/jpg"))
{
$extension=".jpg";
}
else{
$error_message="Image upload failed. Please upload images with the extension .jpg";
}
}//end if error_message
if (!$error_message){
if (is_uploaded_file($imtemp)){
copy($imtemp,$uploaddir . "/" . $newimname . $extension);
unlink($imtemp);
echo "<CENTER>Image Added Successfully!</CENTER><BR>";
} else {
$error_message="Image upload failed. You did not match our security settings!";
}
}
if ($error_message){
die ($error_message);
}
?>
For the inserting it into a database, all you need to do is insert the address to the image, which would be:
Code: Select all
<?php
$imagepath = $uploaddir . "/" . $newimname . $extension;
?>
Posted: Sat Oct 11, 2003 2:52 am
by belovedone
so that will upload my image and insert a new row in my table as well as rename the image file to a unique name?
Posted: Sat Oct 11, 2003 9:14 am
by DuFF
Not exactly, here ill show you some comments. You will have to change some stuff. Like in the $_FILES["imgfile"], the "imgfile" should be the name of the input box for the image in the form. For this script you would put:
Code: Select all
<input type='file' name='imgfile'>
Change this script to make sure the names matchup. You will also obviously need to change the $newimname and $uploaddir so that they are correct for your site. If you want the $newimname to be unique you will have to name the image off something that is unique to it, you can use the filename plus another variable that will make sure that it is unique. You will also need to make sure the mysql query username, password and database are correct.
Code: Select all
<?php
<?php
$imtype=$_FILES["imgfile"]["type"];
$imsize=$_FILES["imgfile"]["size"];
$imname=$_FILES["imgfile"]["name"];
$imtemp=$_FILES['imgfile']['tmp_name'];
$immaxwidth="640";
$immaxheight="480";
$newimname="whateveryouwant"; //unique name for image after uploading
$uploaddir="path/to/any/folder"; //upload folder
if (!$error_message){
$imgsize = GetImageSize($imgfile);
//== check size 0=width, 1=height
if (($imgsize[0] > $immaxwidth) || ($imgsize[1] > $immaxheight)){
$error_message="Image upload failed. Please edit your image to be $immaxwidth x $immaxheight px.";
}
}// end if error_message
if (!$error_message){
if (($imtype == "image/pjpeg") || ($imtype == "image/jpeg") || ($imtype == "image/jpg"))
{
$extension=".jpg";
}
else{
$error_message="Image upload failed. Please upload images with the extension .jpg";
}
}//end if error_message
if (!$error_message){
if (is_uploaded_file($imtemp)){
copy($imtemp,$uploaddir . "/" . $newimname . $extension);
unlink($imtemp);
echo "<CENTER>Image Added Successfully!</CENTER><BR>";
} else {
$error_message="Image upload failed. You did not match our security settings!";
}
}
if ($error_message){
die ($error_message);
}
$imagepath = $uploaddir . "/" . $newimname . $extension;
@mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO table_name VALUES ('','$imagepath')";
mysql_query($query);
mysql_close();
echo "<CENTER>Image Uploaded Successfully!</CENTER><br>";
?>