...

View Full Version : Help with an Upload.php file please



Tiddles
11-08-2010, 04:19 PM
Hey all,

ive been having problems lately, trying to get a page working to upload images to a folder on the server. The code is this:


<body>

<div class='header'>
Admin Managment
</div>

<div class='left'></div>
<div class='right'></div>

<div class='center'>

<form action="" method="post" enctype="multipart/form-data"
name="uploadImage" id="uploadImage">
<p>
<input type="hidden" name="MAX_FILE_SIZE"
value="<?php echo MAX_FILE_SIZE; ?>" />
<label for="image">Upload image:</label>
<input type="file" name="image" id="image" />
</p>
<p>
<input type="submit" name="upload" id="upload"
value="Upload" />
</p>
</form>

<?php
// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 1024 * 500);

if (array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', '/images/');
// replace any spaces in original filename with underscores
$file = str_replace(' ', '_', $_FILES['image']['name']);
// create an array of permitted MIME types
$permitted = array('image/gif', 'image/jpeg', 'image/pjpeg',
'image/png');

// upload if file is OK
if (in_array($_FILES['image']['type'], $permitted)
&& $_FILES['image']['size'] > 0
&& $_FILES['image']['size'] <= MAX_FILE_SIZE) {
switch($_FILES['image']['error']) {
case 0:
// check if a file of the same name has been uploaded
if (!file_exists(UPLOAD_DIR . $file)) {
// move the file to the upload folder and rename it
$success =
----------> move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR .
$file);
} else {
$result = 'A file of the same name already exists.';
}
if ($success) {
$result = "$file uploaded successfully.";
} else {
$result = "Error uploading $file. Please try again.";
}
break;
case 3:
case 6:
case 7:
case 8:
$result = "Error uploading $file. Please try again.";
break;
case 4:
$result = "You didn't select a file to be uploaded.";
}
} else {
$result = "$file is either too big or not an image.";
}
}
?>

And these are the errors im getting when im trying to upload the image:


Warning: move_uploaded_file(/images/GSK.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /clientdata/zeus-dynamic-1/k/a/karriheightsalpacas.com/www/Upload.php on line 84

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/phptmp/phpD6dt8r' to '/images/GSK.jpg' in /clientdata/zeus-dynamic-1/k/a/karriheightsalpacas.com/www/Upload.php on line 84

Line 84 is marked with ------> it isn't actually in the code like that, Its just how I'm distinguishing it from the rest of the code.

Ive made the folder read,write,execute for all in chmod, and im at whits end trying to figure out how to fix this.

if anyone can help i would appreciate it a lot.

Cheers,

Karl Tysoe

abduraooft
11-08-2010, 04:45 PM
Change
define('UPLOAD_DIR', '/images/'); to

define('UPLOAD_DIR', $_SERVER['DOCUMENT_ROOT']. '/images/'); and try again.

DrDOS
11-08-2010, 05:17 PM
At least one other problem, the php code has to be above the form or MAX_FILE_SIZE is undefined, or it has to be defined above the form in a separate php tag. But there's definitely a path name problem too as abduraooft pointed out.

PS: It works, after fixing the MAX_FILE_SIZE problem, making abduraooft's change and making a folder named images in the root of the server with the right permissions it works very well.

Tiddles
11-08-2010, 07:03 PM
Cheers guys, Ive been working on that for a bit, was very frustrating.. don't know how many searches I've had for a fix, and didn't think about asking myself until tonight :/

All works peachy now.

Cheers,

karl



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum