php upload rename problems
Posted: Mon Jun 22, 2009 3:42 pm
I tried looking up past problems and didn't see anything like this.
I'm trying to create an uploader that will resize and rename the photo and dump it back in the same place on my server. I have 4 php files and 1 html file.
This is edited from code my friend had done and it worked great for him but I'm kinda lost at where I've gone wrong editing it.
The html is just a file browser, a box for them to place the last name, and a submit button.
When yhou hit submit the error http://www.razorbacksrealestate.com/fro ... as+invalid.
here is the php
I'm trying to create an uploader that will resize and rename the photo and dump it back in the same place on my server. I have 4 php files and 1 html file.
This is edited from code my friend had done and it worked great for him but I'm kinda lost at where I've gone wrong editing it.
The html is just a file browser, a box for them to place the last name, and a submit button.
When yhou hit submit the error http://www.razorbacksrealestate.com/fro ... as+invalid.
here is the php
Code: Select all
<?php
require('request.class.php');
require('response.class.php');
require('photo-uploader.class.php');
set_error_handler(array('Response', 'makeError'));
$request = new Request;
if ( $request->hasInvalidlast_name() )
{
$response = new Response('Last Name ID was invalid.');
$response->dispatch();
exit;
}
if ( $request->hasInvalidPhoto() )
{
$response = new Response('Uploaded photo was invalid.');
$response->dispatch();
exit;
}
$uploader = new PhotoUploader($request->getPhoto(), $request->getlast_name());
$uploader->resizePhoto();
if ( $uploader->failed() )
{
$response = new Response('Attempt to move uploaded file failed.');
$response->dispatch();
exit;
}
$response = new Response('The photo was uploaded successfully!');
$response->dispatch();
exit;
?>Code: Select all
<?PHP
class Request
{
function hasInvalidlast_name()
{
return empty($_POST['last_name']) || false == ctype_digit($_POST['last_name']);
}
function getlast_name()
{
return $_POST['last_name'];
}
function hasInvalidPhoto()
{
return (
empty($_FILES) ||
(
$_FILES["file"]["type"] != "image/gif" &&
$_FILES["file"]["type"] != "image/jpeg" &&
$_FILES["file"]["type"] == "image/pjpeg"
) ||
$_FILES["file"]["size"] > 2500000 ||
$this->hasPhotoErrorMessage()
);
}
function getPhoto()
{
return $_FILES['file']['tmp_name'];
}
function hasPhotoErrorMessage()
{
$uploadErrors = array(
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
);
if ( isset($uploadErrors[$_FILES['file']['error']]) )
{
trigger_error($uploadErrors[$_FILES['file']['error']]);
return true;
}
return false;
}
}
?>Code: Select all
<?PHP
class Response
{
function Response($msg = '')
{
$this->msg = urlEncode($msg);
}
function makeError($msg = null)
{
static $msgs = array();
if ( empty($msg) )
{
return $msgs;
}
else
{
$msgs[] = $msg;
}
}
function getMsg()
{
if ( $this->msg || $this->makeError() )
{
$msg = '?msg=' . $this->msg;
if ( $this->msg && $this->makeError() )
{
$msg .= urlEncode('; Error: ');
}
$msg .= implode(', ', $this->makeError());
return $msg;
}
}
function dispatch()
{
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/from_david/canadate_test.html' . $this->getMsg());
exit;
}
var $msg;
}
?>Code: Select all
<?PHP
class PhotoUploader
{
function PhotoUploader($filename, $last_name)
{
$this->last_name = $last_name;
if ( $this->cannotMoveUploadedFile($filename) )
{
return $this->failed = true;
}
// Get new dimensions
list($this->width, $this->height) = getImageSize($this->getDestination());
$this->source = imageCreateFromJpeg($this->getDestination());
$this->resized = imageCreateTrueColor(200, $this->getDestinationHeight());
}
function resizePhoto()
{
imageCopyResampled(
$this->resized,
$this->source,
0, 0, 0, 0,
200,
$this->getDestinationHeight(),
$this->width,
$this->height
);
imageJpeg($this->resized, $this->getDestination());
}
function failed()
{
return $this->failed;
}
function getDestination()
{
return getCWD() . $this->last_name . '.jpg';
}
function getDestinationHeight()
{
return ( $this->height * ( 200 / $this->width ) );
}
function cannotMoveUploadedFile($filename)
{
return ( false == move_uploaded_file($filename, $this->getDestination()) );
}
var $failed;
var $source;
var $resized;
var $last_name;
}
?>