Forum Moderators: coopster

Message Too Old, No Replies

problems uploading an image on a remote folder

         

dbarasuk

9:05 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



Hi everyone.

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

d40sithui

10:15 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



you're most likely having a permission issues...althoug i cant be sure since you do not lists the errors you get. if the script works fine on one server, but does not on another, its not the script, but other environmental elements, settings, etc. so what kind of errors are you getting? and also, i see you have the UPLOAD_DIR defined. does that directory exist on the linux server? you have the is_dir() to check for parts of that. make sure when you make a directory that you want people to upload file to, that you make it with the 777 chmod. otherwise, you'll have permission issues. so mkdir(UPLOAD_DIR.$username, 777); would be better suited if you want scripts to be able to save files into it.

dbarasuk

11:55 pm on Dec 12, 2007 (gmt 0)

10+ Year Member



the error I am getting is mentionned in the program, at around 20 lines from the bottom of the script and it's the second one saying :"Error uploading $file, please try again" . In fact the file does not get uploaded at all. it's when $success is false. Also the upload folder is defined in the program and is a subfolder of the "htdocs" folder. It takes also into account of who has logged in

Also the permisions are correctly set to Read, write and execute for the upload directory.

phranque

2:01 am on Dec 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



try adding this to your script:
var_dump($_FILES);
var_dump($success);
if($success) {
...

dbarasuk

2:39 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



phranque,
your function (var_dump) was a bit helpful because it helped to see that the move_oploaded_file did not upload the file in the upload folder; This is what it revealed.
for var_dump($_FILES) the result was:

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]

d40sithui

3:51 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



the var_dump showed that the file was uploaded to a temp directory successful since the error is 0.
try changing your upload directory to "htdocs/Darius/uploads/".

dbarasuk

6:39 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



i tried but could not upload the image file.

Do you think the back slashes used by windows in the path structure may be the cause of this.?

In fact my site is hosted on Linux server while it's developed on a windows machine

Regards

d40sithui

10:37 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



what is your current working directory?
create a simple folder and try to upload it to there or take out the UPLOAD_DIR.$username part and only use the $file as your destination. see if this works.

adwatson

10:54 pm on Dec 13, 2007 (gmt 0)

10+ Year Member



Don't know if it's feasible, but you might also just set up a cron job to run on the linux machine and pull the files from your dev server via ftp/http/scp/whatever might work...