PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I'm building a submission form for uploading files and storing a name in the database... so far I have gotten it to store the title but nothing else. The files don't even upload.
<?php
//This is the directory where images will be saved
$target = "/path/to/upload/";
$target = $target . basename( $_FILES['thumb']['thumbName']['eps1']['eps1Name']['eps2']['eps2Name']['pdf1']['pdf1Name']['pdf2']['pdf2Name']);
//This gets all the other information from the form
$title=$_POST['title'];
$thumb=($_FILES['thumb']['thumbName']);
$eps1=($_FILES['eps1']['eps1Name']);
$eps2=($_FILES['eps2']['eps2Name']);
$pdf1=($_FILES['pdf1']['pdf1Name']);
$pdf2=($_FILES['pdf2']['pdf2Name']);
// Connects to your Database
mysql_connect("localhost", "user", "pass") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO test (title,thumb,eps1,eps2,pdf1,pdf2)
VALUES ('$title', '$thumb', '$eps1', '$eps2', '$pdf1','$pdf2')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['thumb']['tmp_name']['eps1']['tmp_name']['eps2']['tmp_name']['pdf1']['tmp_name']['pdf2']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The files ". basename( $_FILES['uploadedfile']['thumbname']['eps1Name']['eps2Name']['pdf1Name']['pdf2Name']). " have 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.";
}
?>
1. None of those $_FILE array items exist
2. You can't just string a bunch of variables together to define a path or move files, even if they were actually real variables, which they aren't
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
First off, you should really do some validation to make sure that the values you expected exist before you try and save them. Next, the array that is returned in $_FILES contains only a few basic pieces of information. Use 'name' as your first indicator as to whether or not the right information was passed. Once you've verified that the file was selected correctly, you actually need to put code in place to upload that file to the right directory. Once that's finished, you can use the variables to execute your database queries. Here's a quick example that I typed up. I didn't test it but it should work.
daebat wrote:Yeah, I understand I should be using an array? Just not sure how to do it.
No, $_FILES is already an array, your form is fine, the PHP is way way off. There is no var named $_FILES['thumb']['thumbName'], it is $_FILES['thumb']['name']. Also, you need to loop through the files and move them (not tested):
//This is the directory where images will be saved
$target = "/path/to/upload/";
//This gets all the other information from the form
$title=$_POST['title'];
$thumb = ($_FILES['thumb']['name']);
$eps1 = ($_FILES['eps1']['name']);
$eps2=($_FILES['eps2']['name']);
$pdf1=($_FILES['pdf1']['name']);
$pdf2=($_FILES['pdf2']['name']);
// Connects to your Database
mysql_connect("localhost", "user", "pass") or die(mysql_error()) ;
mysql_select_db("database") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO test (title,thumb,eps1,eps2,pdf1,pdf2)
VALUES ('$title', '$thumb', '$eps1', '$eps2', '$pdf1','$pdf2')");
foreach($_FILES as $file) {
move_uploaded_file($file['tmp_name'], $target . $file['name']);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.