I've got a script that will add images to a MySQL database. The image files are on the server and the script takes all of the files in the directory and puts them in the database as blob.
I'm trying to get some error checking to work. One of the issues is that if a file is too big, it does one of two things: It either successfully imports that one file that is too big and then stops with no error; or it imports every other file successfully and stops with no error when it gets to the file that is too big. I'd like to get it to spit out an error in either case so I can see why it is doing it. I'm also puzzled because if the file was too big, it shouldn't ever successfully import, but it does. My code is:
I'm trying to get some error checking to work. One of the issues is that if a file is too big, it does one of two things: It either successfully imports that one file that is too big and then stops with no error; or it imports every other file successfully and stops with no error when it gets to the file that is too big. I'd like to get it to spit out an error in either case so I can see why it is doing it. I'm also puzzled because if the file was too big, it shouldn't ever successfully import, but it does. My code is:
Code:
$path = "/pathtofiles/";
$listing = opendir($path);
while (false !== ($filename = readdir($listing))) {
if ($filename !== "." && $filename !== "..") {
$file = "$path$filename";
$file_size = filesize($file);
$file_type = mime_content_type($file);
$description = substr($filename, 0, strrpos($filename, '.'));
$data = addslashes(fread(fopen($file, "r"), filesize($file)));
$result="INSERT INTO images (description,filedata,filename,filesize,filetype)
VALUES
('$description','$data','$filename','$file_size','$file_type')";
if (!mysql_query($result))
{
echo ("<p><b>Could not add $filename</b>");
} else {
$id= mysql_insert_id();
print "<p>Successfully added $filename: ID <b>$id</b>";
}
}
}