Forum Moderators: coopster
I am new to php and MySql so dont hit me if this is a stupid question.
I have a form that people fill out to upload an image with a title and description. It works great but I would like to on upload of that image rename the file to the unique identifier that realtes to the post. Currently I have it set so it renames to a random number.
I am thinking in the long run for edit/delete of this file it will be better to have it named as the id (example "34.jpg") but I dont know how to do this.
here is my code:
<?php
//Uploaded file location
$uploadDir = '../images/';
// code that will be executed if the form has been submitted:
if(isset($_POST['submit']))
{
$fileName = $_FILES['form_data']['name'];
$tmpName = $_FILES['form_data']['tmp_name'];
$fileSize = $_FILES['form_data']['size'];
$fileType = $_FILES['form_data']['type'];
// the files will be saved in filePath
$filePath = $uploadDir . $fileName;
// get the file extension first
$ext = substr(strrchr($fileName, "."), 1);
// make the random file name
$randName = md5(rand() * time());
// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;
// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
$result= move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$query="INSERT INTO img (title,description,path,name,size,type) ".
"VALUES ('$title','$form_description','$filePath','$fileName','$fileSize','$fileType')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
echo "<br>File uploaded<br>";
}
?>
Thanks for the help!
So what you would have is ....
<?php
$uploadDir = '../images/';if(isset($_POST['submit']))
{
$query="INSERT INTO img (title,description,path,name,size,type) ".
"VALUES ('$title','$form_description','$filePath','$fileName','$fileSize','$fileType')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
}$fileName = $_FILES['form_data']['name'];
$tmpName = $_FILES['form_data']['tmp_name'];
$fileSize = $_FILES['form_data']['size'];
$fileType = $_FILES['form_data']['type'];
$filePath = mysql_insert_id();
$result= move_uploaded_file($tmpName, $uploadDir.$filePath);
if (!$result)
{
echo "Error uploading file";
// if uploading fails then delete the new field just added
mysql_query("DELETE FROM img WHERE id = '".filePath."'");
exit;
}
else
{
echo "<br>File uploaded<br>";
}
?>
This is untested, but i'm sure with a little tweaking you can do something like this ;)
Then instead of using your filePath field in your database table to relate to the image you are using the id of the field to relate to the image.
Hope i have helped,
Del
i used this code and got it to work!
In the original post i sent i forgot about the extension of the image
<?php
$uploadDir = '../images/';
if(isset($_POST['submit']))
{
$fileName = $_FILES['form_data']['name'];
$tmpName = $_FILES['form_data']['tmp_name'];
$fileSize = $_FILES['form_data']['size'];
$fileType = $_FILES['form_data']['type'];$query="INSERT INTO img (title,description,path,name,size,type) ".
"VALUES ('$title','$form_description','$filePath','$fileName','$fileSize','$fileType')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
$filePath = mysql_insert_id();if($fileType=="image/jpeg")
{
$ext=".jpg";
}
elseif($fileType=="image/gif")
{
$ext=".gif";
}
elseif($fileType=="image/png")
{
$ext=".png";
}
else
{
echo "Error uploading file, file type is not allowed";
exit;
}
$result= move_uploaded_file($tmpName, $uploadDir.$filePath.$ext);
if (!$result)
{
echo "Error uploading file";
mysql_query("DELETE FROM img WHERE id = '".$filePath."'");
exit;
}
else
{
echo "<br>File uploaded<br>";
}
}
else
{
echo '<p>Please select a file to upload</p>';
}
?>
Please let me know how you get on ;)
I used this "$ext = substr(strrchr($fileName, "."), 1);"
To get the extention of the file though.
ummmm 2 other questions sorry.
Because of the way that we are doing this there is no point to saving the file path in the DB because the file is cenamed on the server after the path is put in the DB so they dont match right. is that a problem or does it not matter?
and the second question is how do I reed this back out better. I have it working with this script below but it is limited to .jpg no I have stored the file ext in the table under the name "type" but I dont know how to work that in to this code. could u assist me here.
<?php
// Require the database class
require_once('../includes/DbConnector.php');
// Create a new DbConnector object
$connector = new DbConnector();
//image path
$uploadDir = "../images/";
// IMPORTANT! Validate the ID number. See next article
// Execute the query to retrieve the selected article
$result = $connector->query('SELECT id,title,description,path,type FROM img WHERE ID = '.$HTTP_GET_VARS['id']);
// Get an array containing the resulting record
$row = $connector->fetchArray($result);
?>
<?php echo '<span class="small">'.$row['title'].'</span><br><br>';?>
<?php echo '<img src="'."$uploadDir".$row['id'].'.jpg" /><br>';?>
<?php echo '<span class="small">'.$row['description'].'</span>';?>
<?php echo '<span class="small">'.$row['type'].'</span>';?>
<link href="../booth.css" rel="stylesheet" type="text/css" />
Because of the way that we are doing this there is no point to saving the file path in the DB because the file is cenamed on the server after the path is put in the DB so they dont match right. is that a problem or does it not matter?
You are using a hardcoded directory so the DB field is not needed ;)
and the second question is how do I reed this back out better. I have it working with this script below but it is limited to .jpg no I have stored the file ext in the table under the name "type" but I dont know how to work that in to this code. could u assist me here.
On your output you would put ...
<?php
echo '<img src="'."$uploadDir".$row['id'].'.'.$row['type'];
?>
and then on the insert do a validation check on the type and insert the correct type into the DB.
e.g.
if($type=="image/jpg")
{
$insertType = "jpg";
}
if($type=="image/gif"){
$insertType = "gif";
}
Del