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\kde m_db_confi g.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_na me"];
//move it to some temp dir
$base_name = "E:/tmp/".basename($form_d ata);
move_uploaded_file($form_d ata, $base_name);
// read the file
$data = addslashes(fread(fopen($ba se_name, "r"), filesize($base_name)));
//insert query
$imageName = basename($_FILES['userfile ']['name'] );
echo "$imageName";
$query="insert INTO img_pg
VALUES ('$imageName','$data','{$H TTP_POST_F ILES[userf ile][size] }','{$HTTP _POST_FILE S[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.
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
$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,$
$form_data = $HTTP_POST_FILES["userfile
//move it to some temp dir
$base_name = "E:/tmp/".basename($form_d
move_uploaded_file($form_d
// read the file
$data = addslashes(fread(fopen($ba
//insert query
$imageName = basename($_FILES['userfile
echo "$imageName";
$query="insert INTO img_pg
VALUES ('$imageName','$data','{$H
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.
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.tm p"
string(17) "E:/tmp/php1D3.tmp"
what should I do, and what is the reason of this strange error?
Thanks
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.tm
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\kde m_db_confi g.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']['erro r']) {
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_fil esize").") 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_na me"];
//move it to some temp dir
$base_name = "E:/tmp/".basename($form_d ata);
move_uploaded_file($form_d ata, $base_name);
// read the file
$data = addslashes(fread(fopen($ba se_name, "r"), filesize($base_name)));
//insert query
$imageName = basename($_FILES['userfile ']['name'] );
echo "$imageName";
$query="insert INTO img_pg
VALUES ('$imageName','$data','{$H TTP_POST_F ILES[userf ile][size] }','{$HTTP _POST_FILE S[userfile ][type]}') ";
mysql_query($query, $link) or die (mysql_error());
?>
try this ...
<?php
require($_SERVER["DOCUMENT
$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,$
switch ($_FILES['userfile']['erro
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_fil
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
//move it to some temp dir
$base_name = "E:/tmp/".basename($form_d
move_uploaded_file($form_d
// read the file
$data = addslashes(fread(fopen($ba
//insert query
$imageName = basename($_FILES['userfile
echo "$imageName";
$query="insert INTO img_pg
VALUES ('$imageName','$data','{$H
mysql_query($query, $link) or die (mysql_error());
?>
ASKER
any help on that trouble, please?
ASKER
oh sorry wasn't pay attention for your post!
ASKER
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-da ta" 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?
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-da
<!-- 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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...
Bytes...
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
var_dump($form_data);
//move it to some temp dir
$base_name = "E:/tmp/".basename($form_d
var_dump($base_name);
move_uploaded_file($form_d