Link to home
Start Free TrialLog in
Avatar of Ameerh24
Ameerh24

asked on

error when uploading jpg image

hi,
why did this code that upload image to database gives me warning when I'm trying to upload jpg or png files?
the warning is :
Warning: fopen(E:/tmp/) [function.fopen]: failed to open stream: Permission denied in c:\program files\apache group\webroot\image uploadphp.php on line 17
Warning: fread(): supplied argument is not a valid stream resource in c:\program files\apache group\webroot\image uploadphp.php on line 17
large.jpg
but it does not do this when I'm uploading gif file!!
my code is :
<?php
require($_SERVER["DOCUMENT_ROOT"]."\config\kdem_db_config.php");
    $link = @mysql_connect($db_host, $db_user, $db_password);

    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    else echo "Connection made";
    mysql_select_db($db_name,$link);
         $form_data = $HTTP_POST_FILES["userfile"]["tmp_name"];
//move it to some temp dir
         $base_name = "E:/tmp/".basename($form_data);
         move_uploaded_file($form_data, $base_name);
// read the file
         $data = addslashes(fread(fopen($base_name, "r"), filesize($base_name)));
//insert query
$imageName = basename($_FILES['userfile']['name']);
echo "$imageName";
        $query="insert INTO img_pg
                  VALUES ('$imageName','$data','{$HTTP_POST_FILES[userfile][size]}','{$HTTP_POST_FILES[userfile][type]}')";
                  mysql_query($query, $link) or die (mysql_error());
?>
the img_pg table structure is :
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| img_name | varchar(100) | YES  |     | NULL    |       |
| img_data | longblob     | YES  |     | NULL    |       |
| img_size | varchar(60)  | YES  |     | NULL    |       |
| img_type | varchar(60)  | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
notice that I'm using php5 with mysql5 with windows xp.
Avatar of akshah123
akshah123
Flag of United States of America image

tgatif,

I believe the problem is that $base_name is blank for whatever the reason.

Can you try using
var_dump() to get following ...
         $form_data = $HTTP_POST_FILES["userfile"]["tmp_name"];
         var_dump($form_data);
//move it to some temp dir
         $base_name = "E:/tmp/".basename($form_data);
         var_dump($base_name);
         move_uploaded_file($form_data, $base_name);
Avatar of Ameerh24
Ameerh24

ASKER

hi,
it looks strange .. when I upload jpg file I don't get it's $form_data !!
the output of uploading jpg image:
string(0) ""
string(7) "E:/tmp/"
Warning: fopen(E:/tmp/) [function.fopen]: failed to open stream: Permission denied in c:\program files\apache group\webroot\image uploadphp.php on line 23
Warning: fread(): supplied argument is not a valid stream resource in c:\program files\apache group\webroot\image uploadphp.php on line 23
large.jpg

but,when I upload gif file I can get it's $form_data
the output of uploading gif image:
string(26) "C:\WINDOWS\TEMP\php1D3.tmp"
string(17) "E:/tmp/php1D3.tmp"

what should I do, and what is the reason of this strange error?
Thanks
Perhaps the file you are uploading has a size greater than allowed by your php as set in php.ini.

try this ...

<?php
require($_SERVER["DOCUMENT_ROOT"]."\config\kdem_db_config.php");
    $link = @mysql_connect($db_host, $db_user, $db_password);

    if (!$link) {
       die('Could not connect: ' . mysql_error());
    }
    else echo "Connection made";
    mysql_select_db($db_name,$link);

switch ($_FILES['userfile']['error']) {
   case: UPLOAD_ERR_OK
        break;
   case UPLOAD_ERR_INI_SIZE:
       throw new Exception("The uploaded file exceeds the upload_max_filesize directive (".ini_get("upload_max_filesize").") in php.ini.");
   break;
   case UPLOAD_ERR_FORM_SIZE:
       throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
   break;
   case UPLOAD_ERR_PARTIAL:
       throw new Exception("The uploaded file was only partially uploaded.");
   break;
   case UPLOAD_ERR_NO_FILE:
       throw new Exception("No file was uploaded.");
   break;
   case UPLOAD_ERR_NO_TMP_DIR:
       throw new Exception("Missing a temporary folder.");
   default:
       throw new Exception("An unknown file upload error occured");
}
         $form_data = $HTTP_POST_FILES["userfile"]["tmp_name"];
//move it to some temp dir
         $base_name = "E:/tmp/".basename($form_data);
         move_uploaded_file($form_data, $base_name);
// read the file
         $data = addslashes(fread(fopen($base_name, "r"), filesize($base_name)));
//insert query
$imageName = basename($_FILES['userfile']['name']);
echo "$imageName";
        $query="insert INTO img_pg
                  VALUES ('$imageName','$data','{$HTTP_POST_FILES[userfile][size]}','{$HTTP_POST_FILES[userfile][type]}')";
                  mysql_query($query, $link) or die (mysql_error());
?>
any help on that trouble, please?
oh sorry wasn't pay attention for your post!
hi,
I've got this message error:
Fatal error: Uncaught exception 'Exception' with message 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.' in c:\program files\apache group\webroot\image uploadphp.php:20 Stack trace: #0 {main} thrown in c:\program files\apache group\webroot\image uploadphp.php on line 20

and my html form code is :
 <form enctype="multipart/form-data" action="image uploadphp.php" method="POST">

    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />

    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />

    <input type="submit" value="Send File" />
</form>

and my jpg file size is : 45.8 KB

so, how could it be?!!(45 is not bigger than 99999999)
any help on that trouble, please?
ASKER CERTIFIED SOLUTION
Avatar of akshah123
akshah123
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks alot akshah123,
it worked fine..
just one more thing, please..
does the value prop. in <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
denotes to the max file size in bits,bytes,kb?

>>>does the value prop. in <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
Bytes...