kproc
08-17-2006, 10:39 PM
Hi below is code that loads images to a database I'm trying to add code that will check to see how much space the user is using. if total space exceeds 5,000,000 then exit the fuction and give a message. currently I get this error message
Warning: fread(): supplied argument is not a valid stream resource in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 21
Warning: fclose(): supplied argument is not a valid stream resource in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 23
Warning: unlink() [function.unlink]: Permission denied in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 101
Enter Name:
Any id whats wrong
thank you
<?
$owner_id = $_SESSION['user_id'];
include 'db.php';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
if($fileSize > 950000){
echo 'file size is to large';
exit();
}
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($fileSize[0] > 250) || ($fileSize[1] > 200))
{
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpName = tempnam("/tmp", "MKUP");
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $fileName >$tmpName");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$fileName");
/*== remove temp image ==*/
unlink($tmpName);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($fileName))
{
/*== move file to proper directory ==*/
if (!copy($fileName,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
// Check to to make sure space is not exceeded
$check_space = ("SELECT SUM(size) AS size FROM images WHERE owner_id = '$owner_id'");
$space_result = mysql_query($check_space) or die(mysql_error());
while($sum = mysql_fetch_array($space_result)){
$albumSize = $sum['size']; }
$RemainingSpace = (5000000 - $albumSize);
if(albumSize >5000000){
echo 'Exceeded allowable space, contact webmaster to inquire about additional space';
exit();
}
/*== delete the temporary uploaded file ==*/
unlink($tmpName);
$Newname = $_POST['Newname'];
$query = "INSERT INTO images (name, size, type, image, owner_id) ".
"VALUES ('$Newname', '$fileSize', '$fileType', '$content', '$owner_id')";
mysql_query($query) or die('Error, query failed');
}
?>
<form action="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="uploadform">
<table width="203" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="197">Enter Name:<br>
<input class="box" id="Newname" name="Newname" type="text"></td>
</tr>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<tr>
<td> <input name="userfile" type="file" class="box" id="userfile"><br>
<input name="upload" type="submit" class="box" id="upload" value=" Upload "></td></tr>
</table>
</form>
<?
$check_space = ("SELECT SUM(size) AS size FROM images WHERE owner_id = '$owner_id'");
$space_result = mysql_query($check_space) or die(mysql_error());
while($sum = mysql_fetch_array($space_result)){
$albumSize = $sum['size'];
$RemainingSpace = (5000000 - $albumSize);
if(albumSize >5000000){
echo 'Exceeded allowable space, contact webmaster to inquire about additional space';
exit();
}
echo $RemainingSpace;
}
?>
Warning: fread(): supplied argument is not a valid stream resource in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 21
Warning: fclose(): supplied argument is not a valid stream resource in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 23
Warning: unlink() [function.unlink]: Permission denied in C:\MyServer\xampp\htdocs\NewStuff\photos\uploadImage.php on line 101
Enter Name:
Any id whats wrong
thank you
<?
$owner_id = $_SESSION['user_id'];
include 'db.php';
if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
if($fileSize > 950000){
echo 'file size is to large';
exit();
}
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 250 x 200 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($fileSize[0] > 250) || ($fileSize[1] > 200))
{
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpName = tempnam("/tmp", "MKUP");
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $fileName >$tmpName");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$fileName");
/*== remove temp image ==*/
unlink($tmpName);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$final_filename = str_replace(" ", "_", $imgfile_name);
$newfile = $uploaddir . "/$final_filename";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($fileName))
{
/*== move file to proper directory ==*/
if (!copy($fileName,"$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
}
// Check to to make sure space is not exceeded
$check_space = ("SELECT SUM(size) AS size FROM images WHERE owner_id = '$owner_id'");
$space_result = mysql_query($check_space) or die(mysql_error());
while($sum = mysql_fetch_array($space_result)){
$albumSize = $sum['size']; }
$RemainingSpace = (5000000 - $albumSize);
if(albumSize >5000000){
echo 'Exceeded allowable space, contact webmaster to inquire about additional space';
exit();
}
/*== delete the temporary uploaded file ==*/
unlink($tmpName);
$Newname = $_POST['Newname'];
$query = "INSERT INTO images (name, size, type, image, owner_id) ".
"VALUES ('$Newname', '$fileSize', '$fileType', '$content', '$owner_id')";
mysql_query($query) or die('Error, query failed');
}
?>
<form action="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="uploadform">
<table width="203" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="197">Enter Name:<br>
<input class="box" id="Newname" name="Newname" type="text"></td>
</tr>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<tr>
<td> <input name="userfile" type="file" class="box" id="userfile"><br>
<input name="upload" type="submit" class="box" id="upload" value=" Upload "></td></tr>
</table>
</form>
<?
$check_space = ("SELECT SUM(size) AS size FROM images WHERE owner_id = '$owner_id'");
$space_result = mysql_query($check_space) or die(mysql_error());
while($sum = mysql_fetch_array($space_result)){
$albumSize = $sum['size'];
$RemainingSpace = (5000000 - $albumSize);
if(albumSize >5000000){
echo 'Exceeded allowable space, contact webmaster to inquire about additional space';
exit();
}
echo $RemainingSpace;
}
?>