Page 1 of 1

Form Fun!

Posted: Wed Jan 06, 2010 9:34 am
by daebat
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.

Form:

Code: Select all

   <form method="post" action="upload_process.php" enctype="multipart/form-data">
 
            <p>
              Title:
            </p>
            <input type="text" name="title"/>
            <p>
              Thumbnail:
            </p>
            <input type="file" name="thumb"> 
            <p>
            EPS 1:
            </p>
            <input type="file" name="eps1">
            <p>
            EPS 2:
            </p>
            <input type="file" name="eps2">
            <p>
            PDF 1:
            </p>
            <input type="file" name="pdf1">
            <p>
            PDF 2:
            </p>
            <input type="file" name="pdf2">            
            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Submit"/>
          </form>
 
PHP:

Code: Select all

<?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.";
}
?>

Re: Form Fun!

Posted: Wed Jan 06, 2010 12:28 pm
by AbraCadaver
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

Do this to see what $_FILES actually contains:

Code: Select all

echo '<pre>';
print_r($_FILES);
echo '</pre>';

Re: Form Fun!

Posted: Wed Jan 06, 2010 1:52 pm
by daebat
Yeah, I understand I should be using an array? Just not sure how to do it.

Re: Form Fun!

Posted: Wed Jan 06, 2010 2:12 pm
by SimpleManWeb
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.

Code: Select all

 
        if (
        isset($_FILES['thumb']['name']) && 
        isset($_FILES['eps1']['name'])&& 
        isset($_FILES['eps2']['name'])&& 
        isset($_FILES['pdf1']['name'])&& 
        isset($_FILES['pdf1']['name'])) {
            $Target = './'; //The directory location that you want the file uploaded to
            $Names = array('thumb','eps1','eps2','pdf1','pdf2');
            foreach ($Names as $Name) {
                $FileArray = $_FILES[$Name];    
                $File_name = $FileArray['name'];
                $File_size= $FileArray['size'];
                $File = $FileArray['tmp_name'];
                $File_type= $FileArray['type'];
                /*== setup final file location and name ==*/
                /*== change spaces to underscores in filename  ==*/
                $FinalName = str_replace(" ", "_", $File_name);
                $NewFile = $Target . "/$FinalName";
 
                /*== do extra security check to prevent malicious abuse==*/
                if (is_uploaded_file($File)) {
                    /*== move file to proper directory ==*/
                    $Result = copy($File,$NewFile);
                    /*== delete the temporary uploaded file ==*/
                    unlink($File);
                }
                if (!Result) { die('Error Uploading File');}
            }
            $thumb=($_FILES['thumb']['name']);
            $eps1=($_FILES['eps1']['name']);
            $eps2=($_FILES['eps2']['name']);
            $pdf1=($_FILES['pdf1']['name']);
            $pdf2=($_FILES['pdf2']['name']);
        }
 
 
Hope this helps.

Re: Form Fun!

Posted: Wed Jan 06, 2010 2:24 pm
by AbraCadaver
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):

Code: Select all

//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']);
}

Re: Form Fun!

Posted: Wed Jan 06, 2010 2:56 pm
by daebat
Cheers to the both of you! You both gave great answers. Thanks a bunch!

Re: Form Fun!

Posted: Thu Jan 07, 2010 8:12 pm
by SimpleManWeb
Glad that it helped.

Re: Form Fun!

Posted: Fri Jan 08, 2010 3:17 pm
by pickle
The docs are quite helpful too.