pcbytes Posted March 20, 2007 Share Posted March 20, 2007 I am getting the following error in my little applet to upload images to my database. Warning: fread(): supplied argument is not a valid stream resource in C:\apache\htdocs\upload.php on line 4 The script connects to the database no problem. It even creates a new entry. But the entry is blank, and my browser spits out the above mentioned error. Whats wrong in my code? submit.php <form method="post" action="upload.php" enctype="multipart/form-data"> Description:<br> <input type="text" name="form_description" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="10000000"> <br>File to upload:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> upload.php <?php mysql_connect("localhost","jason","password"); mysql_select_db("test"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://192.168.1.66/submit.php> Click Here</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/43430-solved-warning-fread-supplied-argument-is-not-a-valid-stream/ Share on other sites More sharing options...
btherl Posted March 20, 2007 Share Posted March 20, 2007 Replace $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); with $filehandle = fopen($form_data, "r"); if ($filehandle === false) die("Couldn't open $form_data"); $filesize = filesize($form_data); if ($filesize === false) die("Couldn't get filesize for $form_data"); $data = fread($filehandle, $filesize); if ($data === false) die("Couldn't read from filehandle"); $data = addslashes($data); Edit: Actually, you could just use file_get_contents(). Again, check for errors! You might want to check if $form_data is empty first, using if (empty($form_data)) { ... } Link to comment https://forums.phpfreaks.com/topic/43430-solved-warning-fread-supplied-argument-is-not-a-valid-stream/#findComment-210915 Share on other sites More sharing options...
pcbytes Posted March 20, 2007 Author Share Posted March 20, 2007 Thank you so much! Link to comment https://forums.phpfreaks.com/topic/43430-solved-warning-fread-supplied-argument-is-not-a-valid-stream/#findComment-210923 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.