I'm using an html form to submit data directly into a mysql database using a php script. For some reason the pictures will not be uploaded fully into the database. This is when I don't use a multipart/form-data tag. When that occurs the pictures partially upload but when I put the tag in the pictures do not upload at all. Here is the html for the form. I believe that the script works because the pictures partially upload. Any help will be appreciated because I'm getting wicked frustrated. Here is the code for the form with the mutlipart/form-data info:

<div id="slform">

<h1 align="center">Post an Available Sub-Lease</h1>

<form action="signup_ac.php" enctype="multipart/form-data" method="post" name="tempslform"  onsubmit="return validate_form(tempslform) && checkform(email) && validate(tempslform)"> 
<span>Rent:</span> $ <input name="rent" tabindex="1" size="3" maxlength="10" type="text" /><br/>
<span>Listing Title:</span> <input name="listingtitle" tabindex="1" size="30" maxlength="70" type="text" /><br/> 
<span>Street and Cross Street:</span> <input name="stcst" tabindex="1" size="20" maxlength="40" type="text" /><br /> 
<span>Listing Description:</span><br /> 
<textarea name="listdesc" tabindex="1" cols="80" style="width: 60%" rows="10" type="text" align="center"></textarea><br /> 
<span>Reply to: </span> <input name="email" tabindex="1" class="req df" size="30" value="Your email address" maxlength="60" type="text" /> 
<input name="retype" tabindex="1" size="30" value="Retype email address" maxlength="60" type="text" /><br/>
<span>Street:</span>:
<input name="street" tabindex="1" size="20" maxlength="80" type="text" /> 
<span>Nearest Cross Street:</span> <input name="cstreet" tabindex="1" size="20" maxlength="80" type="text" /><br/>
<span>City:</span> <input name="city" tabindex="1" size="20" maxlength="80" type="text" />
<span class="std">State:</span> <input name="state" tabindex="1" size="3" maxlength="10" type="text" /><br/>
<input type="file" name="pic1" /><input type="file" name="pic2" /><br />
<input type="file" name="pic3" /><input type="file" name="pic4" /><br /> 
<button tabindex="1" type="submit" value="submit">Continue</button> </form>
</div>

Thanks ahead of time for any help. If you need any additional information let me know.

Dani AI

Generated

Short summary and what likely caused the symptoms reported by : the form-level encoding and file-permission problem were only part of the picture. The server-side handler must read uploaded files from PHP's $_FILES array (not $_POST), check each file's upload error code, and respect PHP/MySQL size limits. was correct to point at proper file-upload handling, 's alternative (store files on disk and save paths) remains a valid design choice, and 's LONGBLOB approach works when configured correctly.

Checklist (quick diagnostics and fixes)

  • Confirm the form uses enctype="multipart/form-data" and file inputs have distinct names.
  • Inspect the raw upload: add a temporary var_dump($_FILES) to see what the server actually receives.
  • Check each $_FILES['picX']['error'] equals UPLOAD_ERR_OK before reading tmp_name.
  • For BLOB storage, ensure table columns are BLOB/LONGBLOB and MySQL's max_allowed_packet is larger than the biggest file.
  • Verify PHP settings: file_uploads = On, upload_max_filesize and post_max_size are big enough, and memory_limit is adequate if using file_get_contents.
  • If saving to disk, set filesystem permissions/ACLs so the webserver process can write the target directory (Windows: give write rights to IIS/Apache user; Linux: chown/chmod appropriately).
  • Use prepared statements and bind binary data (avoid manual escaping). Validate MIME/type and size before storing.

Example (PDO + bind as LOB)

$files = ['pic1','pic2','pic3','pic4'];
$data = [];
foreach ($files as $f) {
    if (!empty($_FILES[$f]['tmp_name']) && $_FILES[$f]['error'] === UPLOAD_ERR_OK) {
        $data[$f] = file_get_contents($_FILES[$f]['tmp_name']);
    } else {
        $data[$f] = null;
    }
}

$sql = 'INSERT INTO tempslform (pic1,pic2,pic3,pic4) VALUES (:pic1,:pic2,:pic3,:pic4)';
$stmt = $pdo->prepare($sql);
foreach ($files as $f) {
    $stmt->bindValue(':'.$f, $data[$f], PDO::PARAM_LOB);
}
$stmt->execute();

Troubleshooting notes: if only a few bytes are stored that often means the code wrote the filename or a truncated string (check use of $_POST vs $_FILES) or MySQL truncated the packet (check max_allowed_packet). If move_uploaded_file failed earlier, that was a permissions issue and is separate from the server-side reading/insertion logic.

Recommended Answers

All 8 Replies

What do you mean with "partially upload"? Does part of them get into the database? Show the receiving script and the CREATE TABLE statements for the images table.

By partially uploaded I mean 10 bytes of the 100+ for the picture are uploaded. I know it's bad habit to upload pictures directly into the database but for now I would like to do it.

The table is created and contains four longblob binary objects named pic1, pic2, pic3, and pic4. Do you need the create table mysql?

The script for inserting the pictures into the database are as follows:

<?php
include('connect.php');

$tbl_name=tempslform;

$confirmcode = md5(uniqid(rand()));
$rent = $_POST ;
$listingtitle = $_POST ;
$stcst = $_POST ;
$listdesc = $_POST ;
$email = $_POST ;
$retype = $_POST ;
$street = $_POST ;
$ctstreet = $_POST ;
$city = $_POST ;
$state = $_POST ;
$date = date("Y-m-d");
$pic1 = $_POST ;
$pic2 = $_POST ;
$pic3 = $_POST ;
$pic4 = $_POST ;


$sql="INSERT INTO $tbl_name (confirmcode, rent, listingtitle, stcst, listdesc, email, retype, street, ctstreet, city, state, date, pic1, pic2, pic3, pic4) VALUES ('".$confirmcode."', '".$rent."', '".$listingtitle."', '".$stcst."', '".$listdesc."', '".$email."', '".$retype."', '".$street."', '".$ctstreet."', '".$city."', '".$state."', '".$date."', '".$pic1."', '".$pic2."', '".$pic3."', '".$pic4."')";

$result=mysql_query($sql);

?>

I have some other parts about sending an email and confirming the listing but that all works. Currently, when I don't include the multipart/data-form in the form tag, I get a partial upload, a few bytes. When I include it nothing uploads.

Wouldn't it make your database more efficient if you stored the image file in the server and just stored the image's address in the database instead?

That's what I've heard, but I'm setting it up like this right now because I had this in place. If I can't figure it out in a day or two I'll probably try just storing the picture on the server like you said....but this isn't a discussion about whether or not I should save the images in the database or on the server.

Wouldn't it make your database more efficient if you stored the image file in the server and just stored the image's address in the database instead?

Wouldn't it make your database more efficient if you stored the image file in the server and just stored the image's address in the database instead?

You don't make your database more efficient with not storing pictures in it. That is rubbish. You make it more efficient by good design and careful indexing. There are lots of good reasons to have images in the database, one being coding efficiency.
Your upload code is wrong. To load pictures into the database you have to upload them as a file with your $_POST array containing the file name. Then you can read it from the local server into a variable and feed it to the mysql server. Google for "PHP file upload".

I've started to put together a file upload script into a directory as suggested. I created a directory and am trying to upload the picture but I can't due to an error with this appearing:

Warning: move_uploaded_file(pics/signpg.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\hosting\

and another one:

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php64A.tmp' to 'pics/signpg.jpg' in D:\hosting\

I include a connect function that works on other scripts. Any idea what the problem is exactly?

You don't make your database more efficient with not storing pictures in it. That is rubbish. You make it more efficient by good design and careful indexing. There are lots of good reasons to have images in the database, one being coding efficiency.
Your upload code is wrong. To load pictures into the database you have to upload them as a file with your $_POST array containing the file name. Then you can read it from the local server into a variable and feed it to the mysql server. Google for "PHP file upload".

Figured out I had to change permissions on the directory to make it writable. Just wondering if anyone has figured out how to directly put the binary objects into the database so I can upload multiple pictures easily at once.

I've started to put together a file upload script into a directory as suggested. I created a directory and am trying to upload the picture but I can't due to an error with this appearing:

Warning: move_uploaded_file(pics/signpg.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\hosting\

and another one:

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\php64A.tmp' to 'pics/signpg.jpg' in D:\hosting\

I include a connect function that works on other scripts. Any idea what the problem is exactly?

Member Avatar for Member #851298

I think you should go with blob storage if you really wanna put your images into a database.

Here you will find a very simple and detailed tutorial, check it out
(blob_storage).xml

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.