Ok, I need one of you php experts to see if you can tell me what the heck is going on with a php script. I am thinking it's a folder permissions issue, but then again I didn't write this page, just trying to help a friend out.
Here is the error I get when I try to submit a pic for the gallery:
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'testimage.jpg' for writing: Permission denied in C:\Inetpub\vhosts\site.com\httpdocs\admyn\add_photo.php on line 94
Warning: copy(testimage.jpg) [function.copy]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\siteo.com\httpdocs\admyn\add_photo.php on line 96
Could not upload.
Here is the page code:
<?php
session_name ('!c0N1c0!');
session_start();
if (!isset($_SESSION['name'])) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/index.php';
header("Location: $url");
exit();
} else {
require_once('../cgi-bin/mysql_connect.php');
if(isset($_POST['id'])){
$id = $_POST['id'];
}else if(isset($_GET['id'])){
$id = $_GET['id'];
}
if(isset($_POST['gal'])){
$gal = $_POST['gal'];
}else if(isset($_GET['gal'])){
$gal = $_GET['gal'];
}
if (isset($_POST['submitted'])) {
if(!empty($_POST['title'])) {
$title = $_POST['title'];
}
if(!empty($_POST['cat'])) {
$cat = $_POST['cat'];
}
if(!empty($_POST['gal'])) {
$gal = $_POST['gal'];
}
if (isset($_FILES['photo1'])) {
// Validate the type. Should be jpeg or jpg.
$allowed = array ('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/bmp', 'image/x-xbitmap');
if (in_array($_FILES['photo1']['type'], $allowed)) {
if (is_uploaded_file ($_FILES['photo1']['tmp_name'])) {
define(IMAGE_BASE, '../gallery');
define(MAX_WIDTH, 800);
define(MAX_HEIGHT, 600);
# Get image location
$image_file = $_FILES['photo1']['tmp_name'];
$image_path = $_FILES['photo1']['tmp_name'];
# Load image
$img = @imagecreatefromjpeg($image_path);
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
//echo 'width: ' . $width . '<br />';
$height = imagesy($img);
//echo 'height: ' . $height . '<br />';
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
//echo 'scale: ' . $width . '<br />';
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
} else {
echo 'no $img';
}
imagejpeg($img, $_FILES['photo1']['name']);
if (copy($_FILES['photo1']['name'], "../gallery/".$_FILES['photo1']['name']) or die ('Could not upload.')) {
} else { // Couldn't move the file
echo '<p><font color="red">The file could not be moved.</font></p>';
$i = FALSE;
}
$i = $_FILES['photo1']['name'];
} else {
$i = FALSE;
}
} else {
echo '<p><font color="red">Please select .jpg or .jpeg file</font></p>';
}
} else {
$i = FALSE;
}
if($i){
$query = "INSERT INTO `gallery` (`photo`, `title`, `cat_id`, `gal_id`) VALUE ('$i', '$title', '$cat', '$gal')";
$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {
$message = 'The photo has been added.';
}else{
$message = 'The photo could not be added due to a system error.';
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Company - Add Photo</title>
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="../style.css" type="text/css">
<script src="../scripts/png.js"></script>
</head>
<body>
<div id="bgWrapper">
<div id="siteWrapper">
<div id="header"><a href="../index.php"><img src="../images/header_logo.gif" border="0px" alt="Connico - A Rider Levett Bucknall Company" /></a></div>
<div id="nav">
<?php include('nav.html'); ?>
</div>
<div style="clear:both;"></div>
<div id="secWrapper">
<div id="secBg">
<div id="copy">
<h1>View Photos in <?php echo $gallery; ?></h1>
<div id="galleryTopNav">
<a href="view_gallery.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>">Back To Gallery</a>
</div>
<?php
if($message){
echo'<p><font color="RED">' . $message . '</font></p>';
}else{
echo'<p> </p>';
}
?>
<form enctype="multipart/form-data" action="add_photo.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>" method="post">
<?php
$sql = "SELECT * FROM `categories` WHERE `cat_id` = '$id'";
$res = @mysql_query($sql);
$row = mysql_fetch_array($res, MYSQL_ASSOC);
echo'<p><b>Gallery:</b> ' . $row['title'] . '</p>';
?>
<p><b>Photo:</b> <input type="file" name="photo1" /><br>Must be in .jpeg or .jpg format.</p>
<p><b>Caption:</b> <input type="text" name="title" size="45" /></p>
<p><input type="submit" name="submit" value="Submit" /><input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="cat" value="<?php echo $id; ?>" /><input type="hidden" name="gal" value="<?php echo $gal; ?>" /><input type="hidden" name="id" value="<?php echo $id; ?>" /></p>
</form>
</div>
</div>
</div>
<div id="footer">
<?php include('../includes/footer.html'); ?>
</div>
</div>
</div>
</body>
</html>
Here is the error I get when I try to submit a pic for the gallery:
Warning: imagejpeg() [function.imagejpeg]: Unable to open 'testimage.jpg' for writing: Permission denied in C:\Inetpub\vhosts\site.com\httpdocs\admyn\add_photo.php on line 94
Warning: copy(testimage.jpg) [function.copy]: failed to open stream: No such file or directory in C:\Inetpub\vhosts\siteo.com\httpdocs\admyn\add_photo.php on line 96
Could not upload.
Here is the page code:
<?php
session_name ('!c0N1c0!');
session_start();
if (!isset($_SESSION['name'])) {
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1);
}
$url .= '/index.php';
header("Location: $url");
exit();
} else {
require_once('../cgi-bin/mysql_connect.php');
if(isset($_POST['id'])){
$id = $_POST['id'];
}else if(isset($_GET['id'])){
$id = $_GET['id'];
}
if(isset($_POST['gal'])){
$gal = $_POST['gal'];
}else if(isset($_GET['gal'])){
$gal = $_GET['gal'];
}
if (isset($_POST['submitted'])) {
if(!empty($_POST['title'])) {
$title = $_POST['title'];
}
if(!empty($_POST['cat'])) {
$cat = $_POST['cat'];
}
if(!empty($_POST['gal'])) {
$gal = $_POST['gal'];
}
if (isset($_FILES['photo1'])) {
// Validate the type. Should be jpeg or jpg.
$allowed = array ('image/jpeg', 'image/jpg', 'image/pjpeg', 'image/bmp', 'image/x-xbitmap');
if (in_array($_FILES['photo1']['type'], $allowed)) {
if (is_uploaded_file ($_FILES['photo1']['tmp_name'])) {
define(IMAGE_BASE, '../gallery');
define(MAX_WIDTH, 800);
define(MAX_HEIGHT, 600);
# Get image location
$image_file = $_FILES['photo1']['tmp_name'];
$image_path = $_FILES['photo1']['tmp_name'];
# Load image
$img = @imagecreatefromjpeg($image_path);
# If an image was successfully loaded, test the image for size
if ($img) {
# Get image size and scale ratio
$width = imagesx($img);
//echo 'width: ' . $width . '<br />';
$height = imagesy($img);
//echo 'height: ' . $height . '<br />';
$scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
//echo 'scale: ' . $width . '<br />';
# If the image is larger than the max shrink it
if ($scale < 1) {
$new_width = floor($scale*$width);
$new_height = floor($scale*$height);
# Create a new temporary image
$tmp_img = imagecreatetruecolor($new_width, $new_height);
# Copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height);
imagedestroy($img);
$img = $tmp_img;
}
} else {
echo 'no $img';
}
imagejpeg($img, $_FILES['photo1']['name']);
if (copy($_FILES['photo1']['name'], "../gallery/".$_FILES['photo1']['name']) or die ('Could not upload.')) {
} else { // Couldn't move the file
echo '<p><font color="red">The file could not be moved.</font></p>';
$i = FALSE;
}
$i = $_FILES['photo1']['name'];
} else {
$i = FALSE;
}
} else {
echo '<p><font color="red">Please select .jpg or .jpeg file</font></p>';
}
} else {
$i = FALSE;
}
if($i){
$query = "INSERT INTO `gallery` (`photo`, `title`, `cat_id`, `gal_id`) VALUE ('$i', '$title', '$cat', '$gal')";
$result = @mysql_query ($query);
if (mysql_affected_rows() == 1) {
$message = 'The photo has been added.';
}else{
$message = 'The photo could not be added due to a system error.';
}
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Company - Add Photo</title>
<link rel="icon" href="../favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<link rel="stylesheet" href="../style.css" type="text/css">
<script src="../scripts/png.js"></script>
</head>
<body>
<div id="bgWrapper">
<div id="siteWrapper">
<div id="header"><a href="../index.php"><img src="../images/header_logo.gif" border="0px" alt="Connico - A Rider Levett Bucknall Company" /></a></div>
<div id="nav">
<?php include('nav.html'); ?>
</div>
<div style="clear:both;"></div>
<div id="secWrapper">
<div id="secBg">
<div id="copy">
<h1>View Photos in <?php echo $gallery; ?></h1>
<div id="galleryTopNav">
<a href="view_gallery.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>">Back To Gallery</a>
</div>
<?php
if($message){
echo'<p><font color="RED">' . $message . '</font></p>';
}else{
echo'<p> </p>';
}
?>
<form enctype="multipart/form-data" action="add_photo.php?id=<?php echo $id; ?>&&gal=<?php echo $gal; ?>" method="post">
<?php
$sql = "SELECT * FROM `categories` WHERE `cat_id` = '$id'";
$res = @mysql_query($sql);
$row = mysql_fetch_array($res, MYSQL_ASSOC);
echo'<p><b>Gallery:</b> ' . $row['title'] . '</p>';
?>
<p><b>Photo:</b> <input type="file" name="photo1" /><br>Must be in .jpeg or .jpg format.</p>
<p><b>Caption:</b> <input type="text" name="title" size="45" /></p>
<p><input type="submit" name="submit" value="Submit" /><input type="hidden" name="submitted" value="TRUE" /><input type="hidden" name="cat" value="<?php echo $id; ?>" /><input type="hidden" name="gal" value="<?php echo $gal; ?>" /><input type="hidden" name="id" value="<?php echo $id; ?>" /></p>
</form>
</div>
</div>
</div>
<div id="footer">
<?php include('../includes/footer.html'); ?>
</div>
</div>
</div>
</body>
</html>