Hi guys, I'm a PHP noob.
I have a small uploading form, but I get an error when I try to upload something. I don't understand why it's not working, because I use the same code snippet for another upload script, and there it works just fine.
Can anyone help me find the error please? 🙂
This is the error msg:
Warning: imagejpeg() [function.imagejpeg]: Unable to open '/www/htdocs/gal/galleries/1/loeb_aus_p2.jpg' for writing in /www/htdocs/gal/admin/gal_postphotos.php on line 16
Warning: imagejpeg() [function.imagejpeg]: Unable to open '/www/htdocs/gal/galleries/1/zth_loeb_aus_p2.jpg' for writing in /www/htdocs/gal/admin/gal_postphotos.php on line 31
New Photo successfully added!
This is the code:
<?php
include('config.php');
echo $header;
if ($_POST['action'] == "add") {
$uploadedfile = $_FILES['photo']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
list($width,$height) = getimagesize($uploadedfile);
$newwidth = 500;
$newheight = ($height/$width)*$newwidth;
$tmp = imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = $dirpath.$gal_id."/".$_FILES['photo']['name'];
imagejpeg($tmp,$filename,100);
#__ thumb _________________________________________________________
$uploadedthumb = $_FILES['photo']['tmp_name'];
$srcthumb = imagecreatefromjpeg($uploadedthumb);
list($widththumb,$heightthumb) = getimagesize($uploadedthumb);
$newwidththumb = 100;
$newheightthumb = 75;
$src_top = ($src_height / 2) - ($dst_height / 2);
$src_left = ($src_width / 2) - ($dst_width / 2);
$tmpthumb = imagecreatetruecolor($newwidththumb,$newheightthumb);
imagecopyresampled($tmpthumb,$srcthumb,0,0,$src_top,$src_left,$newwidththumb,$newheightthumb,$widththumb,$heightthumb);
$thumbname = $dirpath.$gal_id."/zth_".$_FILES['photo']['name'];
imagejpeg($tmpthumb,$thumbname,100);
#___________________________________________________________________
//free memory, destroying the source's and the pic's canvas
imagedestroy($srcthumb);
imagedestroy($tmpthumb);
imagedestroy($src);
imagedestroy($tmp);
$gal = addslashes($_POST['gal_id']);
$private = addslashes($_POST['private']);
$photo = $_FILES['photo']['name'];
$title = addslashes($_POST['title']);
$description = addslashes($_POST['description']);
$sql = "INSERT INTO $gal_photos (gal,photo,private,title,description) VALUES ('$gal','$photo','$private','$title','$description')";
$query = mysql_query($sql) or die("MySQL Error: <br /> {$sql} <br />". mysql_error());
$num = mysql_affected_rows();
if($num > 0) {
echo "<span class='result'>New Photo successfully added!</span>";
echo "<br /><img src='../galleries/".$gal."/zth_".$photo."' />";
} else {
echo "Error: ".mysql_error();
}
} else {
?>
<strong>Post A New Photo</strong>
<br />
<br />
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="649760" />
<div class='uploadbox'>
Associated Gallery:
<br />
<?
$selgal_sql = "SELECT * FROM $gal_galleries ORDER BY id DESC";
$selgal_result = mysql_query($selgal_sql) or print ("Unable to select data.<br />".$selgal_sql."<br />".mysql_error());
$selgal_num = mysql_num_rows($selgal_result);
if ($selgal_num > 0) {
echo "<select name='gal_id'>";
while ($selgal_row = mysql_fetch_array($selgal_result)) {
$selgal_id = $selgal_row["id"];
$selgal_title = $selgal_row["title"];
echo "<option value='".$selgal_id."'>".$selgal_id.": ".$selgal_title."</option>";
}
echo "</select>";
} else {
echo "<a href='gal_galleries.php'>Please create a new gallery to post photos!</a>";
}
?>
<br />
<br />
Photo File:
<br />
<input type='file' name='photo' size='50' />
<input type="hidden" name="thumb" value="<? ($_POST['photo']) ?>" />
<br />
<br />
Private Photo?: ('yes' - not shown in public gallery)
<br />
<select name="private">
<option value="no">no</option>
<option value="yes">yes</option>
</select>
<br />
<br />
<?
if ($usetitle == "n") {
?>
<input type='hidden' name='title' value='' />
<? } else { ?>
Photo Title:
<br />
<input type='text' name='title' size='50' />
<br />
<br />
<? } ?>
<?
if ($usedesc == "n") {
?>
<input type='hidden' name='description' value='' />
<? } else { ?>
Photo Description:
<br />
<textarea cols='45' rows='5' name='description'></textarea>
<? } ?>
</div>
<br />
<input type="submit" name="action" value="add" />
</form>
<?
}
echo $footer;
?>