PDA

View Full Version : Can I get some help with an upload form?



tyles
09-30-2007, 08:06 PM
I'm new to writing php and learning as I go. I grabbed this from another post here (thank you to whomever wrote it! I can't recall right now). I'm trying to edit it to meet my needs. I need a form to allow my customers to upload images. I then need to be able to retrieve the images and match them up with the customer's order number. I figure if I can store the image name in the db along with the customer order number and information, I can then call it back right? I have the form writing everything except the image name to the db.

The images are stored in a file called 'pictures'. After I get some assistance getting the image name written to the db, I need some help writing the code that I can use to view the images. It doesn't need to be pretty, I just need to see what images go with which order. Each order will likely have multiple images so calling them up by order number would be great.

Here's what I have for my upload.php

<?php

//connect to mysql. You MUST edit these! (except localhost)
mysql_connect("localhost", "***", "**") or die(mysql_error());
mysql_select_db("***") or die(mysql_error());
//upload directory.
//change to fit your need eg. files, upload .... etc.
$upload_dir = "pictures/";
//number of files to upload.
$num_files = 3;
//the file size in bytes.
$size_bytes =2048000; //51200 bytes = 50KB.
//Extensions you want files uploaded limited to.
$limitedext = array(".gif",".jpg",".jpeg",".png");


//check if the directory exists or not.
if (!is_dir("$upload_dir")) {
die ("Error: The directory <b>($upload_dir)</b> doesn't exist");
}
//check if the directory is writable.
if (!is_writeable("$upload_dir")){
die ("Error: The directory <b>($upload_dir)</b> is NOT writable, Please CHMOD (777)");
}


//if the form has been submitted, then do the upload process
//infact, if you clicked on (Upload Now!) button.
if (isset($_POST['upload_form'])){

echo "<h3>Upload results:</h3>";

//do a loop for uploading files based on ($num_files) number of files.
for ($i = 1; $i <= $num_files; $i++) {

//define variables to hold the values.

mysql_query("INSERT INTO files (description, order_number, email, name) VALUES('$description', '$order_number', '$email', '$name') ") or die(mysql_error());
$new_file = $_FILES['file'.$i];
$description = $_POST['description'];
$order_number = $_POST['order_number'];
$email = $_POST['email'];
$name = $_POST['name'];
$file_name = $new_file['name'];
//to remove spaces from file name we have to replace it with "_".
$file_name = str_replace(' ', '_', $file_name);
$file_tmp = $new_file['tmp_name'];
$file_size = $new_file['size'];

#-----------------------------------------------------------#
# this code will check if the files was selected or not. #
#-----------------------------------------------------------#

if (!is_uploaded_file($file_tmp)) {
//print error message and file number.
echo "File $i: Not selected.<br>";
}else{
#-----------------------------------------------------------#
# this code will check file extension #
#-----------------------------------------------------------#

$ext = strrchr($file_name,'.');
if (!in_array(strtolower($ext),$limitedext)) {
echo "File $i: ($file_name) Wrong file extension. <br>";
}else{
#-----------------------------------------------------------#
# this code will check file size is correct #
#-----------------------------------------------------------#

if ($file_size > $size_bytes){
echo "File $i: ($file_name) Failed to upload. File must be <b>". $size_bytes / 1024 ."</b> KB. <br>";
}else{
#-----------------------------------------------------------#
# this code check if file is Already EXISTS. #
#-----------------------------------------------------------#

if(file_exists($upload_dir.$file_name)){
echo "File $i: ($file_name) already exists.<br>";
}else{
#-----------------------------------------------------------#
# this function will upload the files. :) ;) cool #
#-----------------------------------------------------------#
if (move_uploaded_file($file_tmp,$upload_dir.$file_name)) {
echo "File $i: ($file_name) Uploaded.<br>";
}else{
echo "File $i: Faild to upload.<br>";
}#end of (move_uploaded_file).

}#end of (file_exists).

}#end of (file_size).

}#end of (limitedext).

}#end of (!is_uploaded_file).

}#end of (for loop).
# print back button.
echo "<a href=\"$_SERVER[PHP_SELF]\">back</a>";
////////////////////////////////////////////////////////////////////////////////
//else if the form didn't submitted then show it.
}else{
echo " <h3>Select files to upload!.</h3>
Max file size = ". $size_bytes / 1024 ." KB";
echo " <form method=\"post\" action=\"$_SERVER[PHP_SELF]\" enctype=\"multipart/form-data\">";
// show the file input field based on($num_files).
for ($i = 1; $i <= $num_files; $i++) {
echo "File $i: <input type=\"file\" name=\"file". $i ."\"><br>";
}
echo " <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"$size_bytes\">

Name: <input type=\"text\" name=\"name\"><br />
Order Number: <input type=\"text\" name=\"order_number\"><br />
Email: <input type=\"text\" name=\"email\"><br />
<br /><br />
File Description: <textarea name=\"description\"></textarea><br />
<input type=\"submit\" name=\"upload_form\" value=\"Upload Now!\">
</form>";
}
?>

Am I correct in that this is the part I need to edit in order to write the image name to the db? I'm thinking it's $file_name part, though I could be totally wrong.

mysql_query("INSERT INTO files (description, order_number, email, name) VALUES('$description', '$order_number', '$email', '$name') ") or die(mysql_error());
$new_file = $_FILES['file'.$i];
$description = $_POST['description'];
$order_number = $_POST['order_number'];
$email = $_POST['email'];
$name = $_POST['name'];
$file_name = $new_file['name'];