PHP upload failing
Posted: Sun Jul 08, 2007 6:49 pm
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
am trying to upload an image using a html form and PHP and it seems to be failing at the move_uploaded_file i get a returned message of here was an error uploading the file, please try again!
I want to have thisCode: Select all
#Start the PHP session
session_start();
#Variables set to log into SQL database
$SQLusername = 'xxxx';
$SQLpassword = 'xxx';
$SQLdbname = 'xxxxx';
$db = mysql_connect("localhost", $SQLusername, $SQLpassword) or print mysql_error ();
mysql_select_db($SQLdbname) or print mysql_error ();
#Checks to see if user is logged in
if($_SESSION['sesLogIn'] != 1) {
header("Location: http://www.domain.com/admin/");
exit();
}
$ID = $_GET['ID'];
$siteID = $_GET['siteID'];
$uploaddir = '/homes/html/images/Temp/';
$uploadfile = $uploaddir . basename($_FILES['newMap']['name']);
$uploadfile2 = $uploaddir . basename($_FILES['newLogo']['name']);
#upload image function
function createImage($imgname,$maxh,$maxw,$newname){
list($width, $height) = getimagesize($imgname);
if ($width < $maxw && $height < $maxh) {
$new_height = $height;
$new_width = $width;
} else {
$ratio = ($width/$height);
$new_height = sqrt(10000/$ratio);
$new_width = $width * ($new_height / $height);
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($imgname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Grab new image
ob_start();
ImageJPEG($image_p);
$image_buffer = ob_get_contents();
ob_end_clean();
ImageDestroy($image_p);
//Create temporary file and write to it
$fp = tmpfile();
fwrite($fp, $image_buffer);
rewind($fp);
}
//#Resize Image based on maximum width and maximum height
function resizeBoth($imgname,$maxh,$maxw,$newname) {
list($width, $height) = getimagesize($imgname);
if ($width < $maxw && $height < $maxh) {
$new_height = $height;
$new_width = $width;
} if ($width > $height) {
$new_height = $height * ($maxw / $width);
$new_width = $maxw;
} else {
$new_height = $maxh;
$new_width = $width * ($maxh / $height);
}
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($imgname);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Grab new image
ob_start();
ImageJPEG($image_p);
$image_buffer = ob_get_contents();
ob_end_clean();
ImageDestroy($image_p);
//Create temporary file and write to it
$fp = tmpfile();
fwrite($fp, $image_buffer);
rewind($fp);
}
if(array_key_exists('uploadNewLogo', $_POST)) {
if ($ID != "" && $ID != "new") {
if(move_uploaded_file($_FILES['newLogo']['tmp_name'], $uploadfile2)) {
echo "The file ". basename($_FILES['newLogo']['tmp_name'][name]). "has been uploade";
}else{
echo "There was an error uploading the file, please try again!";
}
}
}
form enctype="multipart/form-data" action="<? print $_SERVER['PHP_SELF'] . "?ID=" . $ID ?>" method="POST">
</tr>
<tr>
<td align="right" valign="top"><div align="right">Logo:</div></td>
<td align="left" valign="top"><?
if ($ID != "" && $ID != "new") {
if (file_exists("/homes/html/images/browse/logos/" . $ID . ".jpg")) {
print "<img src='http://homes.pacificscene.com/images/browse/logos/" . $ID . ".jpg' />";
print "<input type='submit' name='deleteImage' value='Delete Logo' />";
print "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"30000\" />";
print "<br />";
}
print "<input name='newLogo' type='file' size='40' /><input type='submit' name='uploadNewLogo' value='Upload' />";
}
?>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]