user188962
user188962

Reputation:

How to make users able to upload images in a form?

I have a classifieds website where users must fill in a form in order to put a ad. The form consists of name, password, category, specifications etc etc.

Now, I need to add a image upload function into this form, which must have the following:

1- Upload up to 5 images. 2- A 'remove image link' beneath each image if the user wants another image instead.

How would you do this?

Thanks

Best would be if there was a plugin or something to Jquery which is easy to modify...

Upvotes: 0

Views: 350

Answers (2)

mjdth
mjdth

Reputation: 6536

If you want to upload it as a file to an SQL database, have something like this in the form (in php echo format):

echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"2000000\">
<input name=\"userfile\" type=\"file\" id=\"userfile\">";

Then in your form's receiving php page put something like this:

if ($_FILES['userfile']['size'] > 0) {

    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];

    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }



    $query = "INSERT INTO files (name, size, type, content ) VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

    mysql_query($query) or die('Error, query failed'); 


    $thisq = mysql_query("SELECT * FROM `files` WHERE 1 ORDER BY `id` DESC LIMIT 1");
    $fileidnumber= mysql_fetch_array($thisq);

}

That will store the file to a database and then return the key for you to save or use however you'd like. You can then create a page to download the files like this:

<?php
import_request_variables(gp);

if(isset($_GET['id'])) {
    // if id is set then get the file with the id from database
    $id    = $_GET['id'];
    $query = "SELECT name, type, size, content " .
             "FROM `files` WHERE id = '$id'";

    $result = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $content) = mysql_fetch_array($result);

    header("Content-length: $size");
    header("Content-type: $type");
    header("Content-Disposition: attachment; filename=$name");
    echo $content;

    exit;
}

?>

Upvotes: 0

Jordan Running
Jordan Running

Reputation: 106027

jQuery Multiple File Upload

You can limit the number of uploads using the max option or passing a number as the only parameter. More info on the Examples tab.

Upvotes: 1

Related Questions