Forum Moderators: coopster
I have a long script to upload images in an upload directory.
My development machine is a windows one and I hosted my website on a linux server.
On my machine, the script is faultless because all the images go into the right upload folder. But this does not work when I use the VERY SAME script to upload an image from my machine to the location of the server.
Any idea of how to solve that?
The script is the following:
<?php include('../includes/logout_script.inc.php');
// protect this upload page by a session
session_start();
if(!isset($_SESSION['authenticated'])) {
// redirect user to login
header('Location: ../HTML/login.php');
exit;
}
// define Maximum upload file size
define('MAX_FILE_SIZE', 768000); // originally 51200 bytes or 50 KB.
if(array_key_exists('upload', $_POST)) {
// define constant for upload folder
define('UPLOAD_DIR', 'C:/htdocs/Darius/uploads/');
// replace any spaces in original filename with underscores
// at the same time assign to a simpler variable
$file = str_replace(' ', '_', $_FILES['image']['name']);
// convert the max size to kilobytes
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted types
$permitted =array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
// begin by assuming the file is unacceptable
$sizeOK = false;
$typeOK = false;
// check that file is within the permitted size
if($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
$sizeOK = true;
}
foreach($permitted as $type) {
if($type== $_FILES['image']['type']) {
$typeOK = true;
break;
}
}
if($sizeOK && $typeOK) {
switch($_FILES['image']['error']) {
case 0:
// username comes from a session variable
$username = $_SESSION['authenticated'];
if($username) {
// create an upload folder for the user if it does not exist yet
if(!is_dir(UPLOAD_DIR.$username)) {
mkdir(UPLOAD_DIR.$username);
}
}
// make sure file of same name has not been uploaded
if(!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$file);
}
else { // get date and time
ini_set('date.timezone', 'Europe/Brussels');
$now = date('Y-m-d-His');
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now. $file);
}
if($success) {
$result = "$file uploaded successfully.";
}
else {
$result = "Error uploading $file. Please, try again!";
}
break;
case 3: // case 3 concerns incomplete uploads.
$result = "Error uploading $file. Please, try again.";
default:
// this concerns error levels 5, 6 & 7 which are system errors.
$result = "System error uploading $file. Contact webmaster.";
}
}
elseif($_FILES['image']['error']== 4) {
$result = "No file selected.";
}
else {
// this concerns error levels 1 & 2 when size too big than $max
$result = "$file cannot be uploaded. The maximum size of the image that you can upload is: $max. Acceptable file types: gif, jpg, png";
}
}
?>
On the remote server, I replaced the upload directory with the name of my website as indicated by the admin of the server, but with success.
How can please solve that problem.
If you want to test the script, use the next link (but this is possible only if one has already registered)
Kind regards
Also the permisions are correctly set to Read, write and execute for the upload directory.
array(1) { ["image"]=> array(5) { ["name"]=> string(23) "darius_in_the_class.jpg" ["type"]=> string(11) "image/pjpeg" ["tmp_name"]=> string(14) "/tmp/phpDFjz74" ["error"]=> int(0) ["size"]=> int(201034) } }
var_dump($success) the result was:
NULL which means no upload took place.
I think this has something to do with the upload directory since the image size conforms with the max_file_size and expected type ('image/pjpg')
offline, the upload file was 'C:/htdocs/Darius/uploads/' and performed with no problem.
I was advised by the hosting company that I should replace the 'C:/' by the site name.
Since my site is :http://www.darius.example.com,
I defined the upload dir as 'http://www.darius.bysthost13.com/htdocs/Darius/uploads/' believing it would work but in vain.
Any other possible solution?
Regards,
[edited by: jatar_k at 2:50 pm (utc) on Dec. 13, 2007]
[edit reason] please use example.com [/edit]