how deposit a picture to MYSQL database
below is the code :

<?php
$id=$_POST;
$photo=$_POST;
if ($photo<>""){
$fp=fopen($photo,"r");
$data=addslashes(fread($fp,filesize($photo)));
}
$sql="insert into product(id,pic) values('$id','$data')";
mysql_query($sql,$conn) or die ("插入数据失败: ".mysql_error());
?>

but always unsuccessful! why? plz help me!

Dani AI

Generated

A short, practical add-on to the thread that complements ’s followups and the pointers from and : a reliable pattern, a minimal example, and common host-side pitfalls to check when uploads "work locally but fail on the host."

Keep uploads simple and safe: prefer saving image files to a controlled directory and storing a small metadata record in the database (filename, original name, MIME, size, upload timestamp). This avoids large-row I/O inside MySQL and makes serving, caching, and backups easier. If DB storage is required, use parameterized statements and a LONGBLOB for bigger images.

Example (save file on disk and insert metadata with PDO):

<?php
// $tmpPath = temporary uploaded file path (from the upload handler)
// $origName = original filename
$uploadsDir = __DIR__ . '/uploads';
if (!is_dir($uploadsDir)) mkdir($uploadsDir, 0755, true);

$filename = bin2hex(random_bytes(12)) . '.' . pathinfo($origName, PATHINFO_EXTENSION);
$target = $uploadsDir . '/' . $filename;

if (!move_uploaded_file($tmpPath, $target)) { /* handle error */ }

$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('INSERT INTO images (filename, original_name, mime, size) VALUES (?, ?, ?, ?)');
$stmt->execute([$filename, $origName, mime_content_type($target), filesize($target)]);
?>

Troubleshooting checklist (hosts often differ): confirm upload size limits in php.ini (upload_max_filesize, post_max_size), ensure the script has write permission to the uploads directory, verify no mod_security rules or open_basedir restrictions block the write, and enable error logging to capture failed moves. Security notes: validate the MIME and image headers (don’t trust client filename/MIME), generate non-guessable filenames, store uploads outside webroot or block execution in the uploads folder, and restrict accepted types and sizes.

Look up the super global $_FILES on php.net. Your file and it's info are in it. Assuming your form is correct.

Not only that you must store the picture as Binary or Blob data type, and you need more than just the data, such as extension(to deteremine what compression is used), and the size.

I suggest you search on google, there are plent of great articles on the net for upload images and stuff to databases.

thanks a lot

when i test this code on my own computer,it is successful.
but when i upload this code to special PHP space it is unsuccessful
why? are you have the same functional code?

pls help me!!!!

Home test beds are useless for development IMO. For that matter, only one server for development can bite ya in the code.

You need to test and develop on a real server and test on more then one server for the best results.

thanks a lot noppid !!

i have resolved my question !

" Look up the super global $_FILES on php.net.
Your file and it's info are in it. Assuming your form is correct. "

your this word enlighten me!!
thanks!

It's good to hear you were able to use the reference and solve your problem.

Great work!

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.