Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

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.
0
Ameerh24
Asked:
Ameerh24
  • 5
  • 4
1 Solution
 
akshah123Commented:
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);
0
 
Ameerh24Author Commented:
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
0
 
akshah123Commented:
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());
?>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ameerh24Author Commented:
any help on that trouble, please?
0
 
Ameerh24Author Commented:
oh sorry wasn't pay attention for your post!
0
 
Ameerh24Author Commented:
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?
0
 
akshah123Commented:
Well, try taking out the
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
altogether.
0
 
Ameerh24Author Commented:
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?

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now