Form Fun!

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!

Moderator: General Moderators

Post Reply
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Form Fun!

Post 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.";
}
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Form Fun!

Post 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>';
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.
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Form Fun!

Post by daebat »

Yeah, I understand I should be using an array? Just not sure how to do it.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Form Fun!

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Form Fun!

Post 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']);
}
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.
daebat
Forum Commoner
Posts: 47
Joined: Mon May 11, 2009 10:16 am

Re: Form Fun!

Post by daebat »

Cheers to the both of you! You both gave great answers. Thanks a bunch!
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Form Fun!

Post by SimpleManWeb »

Glad that it helped.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Form Fun!

Post by pickle »

The docs are quite helpful too.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply