Upload path syntax in script - help!

I’ve just moved a website from a development server to its new home on a VPS.
The PHP image upload script was working fine on the old server, but is failing on the new server.

The path on the server to the target dir is:
C:\Inetpub\wwwroot\[doman].com\user_images\

Here’s the syntax of the of the upload portion of the script that is not working (reports a failed message from the script).
Anyone see anything wrong with this???

$upload_class = new File_Upload(‘C:\\Inetpub\\wwwroot\\[domain].com\\user_images\\’,512000,‘0’,‘upload’);

Notes: [domain].com of course is actually the domain name of the website
and the strings following the path are the parameters for the script.

Server Environment:
Windows Server 2003 (64bit)
PHP 5.3x

Thanks,

  • Michael

Without an actual error message from PHP only some soothsayer can help you.

What a terrible VPS it should be - a windows based one

I appreciate the input - and hope to be able to provide an error log entry soon… however, the question was, is the PATH SYNTAX correct - ie: using the double slash (to escape the dir separator).

Thanks,

  • Michael

yes, it seems to be correct.
unfortunately, i have no PHP interpreter in my head, so I can’t run this code by looking at it and answer your question for sure.
but you can check it yourself, just by printing this line out

I wonder if it’s a “real” vs. “virtual” path thing. You are using syntax for the “real” filesystem path, but maybe you need to use the “virtual” - i.e. HTTP - path. Maybe something like

$upload_class = new File_Upload(‘/user_images/’,512000,‘0’,‘upload’);

maybe you need to use the “virtual” - i.e. HTTP - path.

for the filename? you kidding?

errmmm…

  1. Why do you think it’s the path that caused your problem?
  2. Do you have C:\PHP\ folder on your hard disk?

Does the server have both php4 and php5 installed on it? If so try phpinfo() again but in a file with a .php5 extenstion, the server may be configured to process files that are .php with php version 4.x and files that are .php5 with php version 5.x

Then your script isn’t working, you have to debug it, not “eliminate obvious errors”.
You have an error message. Why do you run for anything but not this error message?
You’re waste your own time and other people’s time, by asking irrelevant questions.

You’re going totally wrong way.
There is the only one way to repair a program - debugging. Maximum error reporting and step by step execution.
If you’re not a programmer, you’d better hire one.

reports a failed message from the script

What does it say exactly?

It returns the error message for case 1: “There was an error uploading the file to the server.”

##--------------------------------------------
##ERROR FUNCTION
##Returns error messages
##--------------------------------------------
function error($num){
switch($num){
case ‘1’:
$err = “There was an error uploading the file to the server.”;
break;
case ‘2’:
$err = "The uploaded file was too large - max size is ".round($this->file_max_size/5120).“kb”;
break;
case ‘3’:
$err = “This server does not accept the file type you attempted to upload.”;
break;
case ‘4’:
$err = “The uploaded file could not be moved from its temporary location.”;
break;
case ‘5’:
$err = “The image dimensions were too big.”;
break;
case ‘6’:
$err = “This type of image cannot be manipulated.”;
break;
case ‘7’:
$err = “An error occured when resizing the image.”;
break;
}

Amazing.
upload path going to use only at [4] but we already have an error at [1].
It is hard to find a black cat in a dark room, when the cat isn’t there.

Now time to check condition that set this type of error.
Also these lines at the top of the script would be mighty helpful:

ini_set('display_errors',1);
error_reporting(E_ALL);

And then pray for no @ operators used in the code

Just discovered that I did indeed screw up the installation of PHP and have 2 different PHP.INI files - one in C:\Windows and one in C:\PHP. The one in C:\windows\ is what is being called. I turned gloabls ‘on’, but am still getting the same result.

  • michael

Understood and agreed. I was trying to avoid taking up a lot of space in a message with the entire script - but it’s a great (old) script and maybe others will find it useful:

<?php

###################################################################################################

File Upload Class - v1.0

http://www.akvoid.com

Written By startx - Jacob Wyke

startx@aknetwork.org

###################################################################################################

This class handles all your file uploading needs. It can handle most file types - you can add

more if needed (just add them to the array in the file_types() function. To use this class

for yourself do the following:

USAGE:

##-------------------------------------------------------------------------------------------------

include(‘this_file_name.php’);

$upload_class = new File_Upload(‘upload directory’,

‘max_file_size’,

‘file__types_array’,

‘upload_field_name’,

‘upload_fix_prefix’

);

if(!$_POST[submit]){

#shows the default upload form included with the class - you can make your own

echo $upload_class->default_form(‘file_upload.php’);

}else{

$image[width] = ‘150’; #max image width

$image[height] = ‘350’; #max image height

$image[thumbs] = ‘0’; #make thumbnail of image

$image[thumb_width] = ‘50’; #thumbnail width

$image[thumb_height] = ‘50’; #thumbnail height

$image[resize] = ‘1’; #should images be automatically resized

$image[skip_resize] = ‘0’; #skip image resizing - shows no “image too big” error

#processes the submitted form and returns any error or success message

echo $upload_class->process($image);

#array containing the location of all uploaded files so you can add them to a database/file etc

$uploaded_files = $upload_class->output;

}

##-------------------------------------------------------------------------------------------------

The example above shows you how to control the class if you wish to only upload images,

if you want to just upload files to the server then do the same except exclude all the

$image array vars and DO NOT pass it to the “echo $upload_class->process($image);” line

e.g.

echo $upload_class->process();

Further explainations for calling the class

‘upload directory’ - The directory where you want to store the uploaded files

Make sure you make it writable by the server.

‘max_file_size’, - This is the maximum allowed file size to be uploaded in bytes.

Default is set to 51200 which is 500kb

‘file_types_array’ - This is an array containing your own file types. By default

the script allows all the file types shown in the file_types() function

but if you pass your own array of them - then only those types are

allowed. This allows you to limit the types of files. You could of

course just delete any unwanted types from the array in file_types()

‘upload_field_name’ - This is the name of the file field in the form - only use if you

make your own form and dont use the one included in this class.

‘upload_fix_prefix’ - Allows you to add a prefix to all files you upload so that you

can better organise them.

All of these are optional and so you can just call the class with ‘new File_Upload();’

###################################################################################################

LICENSE

Please feel free to use/edit this class as you wish but for non-commercial uses ONLY. If

you wish to use it for a commercial product that will be sold then please contact me

further for usage details. You must also keep this license and my contact details attached

somewhere to this class file.

Other than that please feel free to use and play with it and give me feedback and info

on what your using it for :slight_smile:

Enjoy!

Jacob Wyke - startx@aknetwork.org

###################################################################################################

class File_Upload{

var $allow_types;
var $field_name;
var $dir;
var $file_prefix;
var $file_max_size;
var $location;
var $thumb_location;
var $output;

##--------------------------------------------
##CLASS CONSTRUCTOR
##--------------------------------------------
function File_Upload($dir = ‘uploads’, $size = ‘512000’, $types = ‘0’, $name = ‘upload’){
#creates array of allowed file types if you dont enter your own
if($types == ‘0’){
$this->file_types();
}else{
$this->file_types = $types;
}

  $this-&gt;field_name = $name;     		#name of the upload field in form
  $this-&gt;dir = $dir;             		#name of upload directory
  $this-&gt;file_prefix = $_POST['uid'];     	#file prefix
  $this-&gt;file_max_size = $size; 		#max file size
  $this-&gt;output = array ();     		#makes array to store loction of uploaded files so you know what was uploaded

}

##--------------------------------------------
##FILE TYPES FUNCTION
##Fills the array with the types of files you want to allow
##You can add your own list of file types to the array below
##--------------------------------------------
function file_types(){
$this->allow_types = array (
‘image/gif’,
‘image/x-png’,
‘image/jpeg’,
‘image/pjpeg’,
);
}

##--------------------------------------------
##PROCESS FUNCTION
##Processes the file upload
##--------------------------------------------
function process($image = ‘0’){
#checks that file was uploaded
if(!file_exists($_FILES[$this->field_name][tmp_name])){
return $this->error(1);
}else{
#checks that the file isnt too large
if($_FILES[$this->field_name][size]>$this->file_max_size){
return $this->error(2);
}else{
#checks to see if file type is allowed
if(!in_array($_FILES[$this->field_name][type],$this->allow_types)){
return $this->error(3);
}else{

        	if($_FILES[$this-&gt;field_name][type]=='image/gif')
        		$ext='.gif';
        	if($_FILES[$this-&gt;field_name][type]=='image/x-png')
        		$ext='.png';
        	if($_FILES[$this-&gt;field_name][type]=='image/jpeg')
        		$ext='.jpg';
        	if($_FILES[$this-&gt;field_name][type]=='image/pjpeg')
        		$ext='.jpg';

           #Repeat loop till you find an available file name
           $loop = '0';
           do{
              $loop++;
              #create new file location
              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix.".jpg"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix.".jpg");
              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix.".gif"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix.".gif");
              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix.".png"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix.".png");

              $this-&gt;location = $this-&gt;dir."/".$this-&gt;file_prefix.$ext;

              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix."_t".".jpg"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix."_t".".jpg");
              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix."_t".".png"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix."_t".".png");
              if(file_exists($this-&gt;dir."/".$this-&gt;file_prefix."_t".".gif"))
              	unlink($this-&gt;dir."/".$this-&gt;file_prefix."_t".".gif");

              $this-&gt;thumb_location = $this-&gt;dir."/".$this-&gt;file_prefix."_t".$ext;
           }while(file_exists($this-&gt;location));

           #processes file for image stuff
           if($image){
              $outcome = $this-&gt;process_image($image);
              if(!$image[skip_resize]){ #dont need to rename the orignal as we have skipped the resizing
                 $this-&gt;location = $this-&gt;dir."/orig_".$loop."_".$this-&gt;file_prefix.$_FILES[$this-&gt;field_name][name];
              }
           }

           if($outcome){
              #returns any errors from the image function
              return $outcome;
           }else{
			  return "1";
           }
        }
     }
  }

}

##--------------------------------------------
##PROCESS IMAGE FUNCTION
##Processes images
##--------------------------------------------
function process_image($image){
#gets the image dimensions
$size = getimagesize($_FILES[$this->field_name][tmp_name]);
#makes sure the image isnt larger than your max width/height
if((($size[0]>$image[width]) OR ($size[1]>$image[height])) AND !$image[resize] AND !$image[skip_resize]){
return $this->error(5);
}else if(($size[0]>$image[width]) OR ($size[1]>$image[height]) AND $image[resize]){
#resize the image if wanted
if(!$image[skip_resize]){
$height=(int)(($image[width]*$size[1])/$size[0]);
$this->make_image($this->location, $image, $size, $image[width], $height);
}
}else{
#send image as is if no resize is needed
$this->make_image($this->location, $image, $size, $size[0], $size[1]);
}

  #make thumbnail of image if needed
  if($image[thumbs]){
     $this-&gt;make_image($this-&gt;thumb_location, $image, $size, $image[thumb_width], $image[thumb_height]);
  }

}

##--------------------------------------------
##MAKE IMAGE FUNCTION
##Makes image
##--------------------------------------------
function make_image($location, $image, $size, $width, $height){
if($_FILES[$this->field_name][type]==‘image/gif’){
if(function_exists(‘imagecreatefromgif’)){
$im = @imagecreatefromgif($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}
if($_FILES[$this->field_name][type]==‘image/jpeg’ OR $_FILES[$this->field_name][type]==‘image/pjpeg’){
if(function_exists(‘imagecreatefromjpeg’)){
$im = @imagecreatefromjpeg($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}
if($_FILES[$this->field_name][type]==‘image/x-png’){
if(function_exists(‘imagecreatefrompng’)){
$im = @imagecreatefrompng($_FILES[$this->field_name][tmp_name]);
}else{
echo $this->error(6);
exit;
}
}

  if(!$im){
        echo $this-&gt;error(7);
        exit;
  }else{
     $dst_img = imagecreatetruecolor($width,$height);
     imagecopyresized($dst_img, $im, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
     imagejpeg($dst_img, $location, 75);
     array_push($this-&gt;output, $location);
  }

}

##--------------------------------------------
##ERROR FUNCTION
##Returns error messages
##--------------------------------------------
function error($num){
switch($num){
case ‘1’:
$err = “There was an error uploading the file to the server.”;
break;
case ‘2’:
$err = "The uploaded file was too large - max size is ".round($this->file_max_size/5120).“kb”;
break;
case ‘3’:
$err = “This server does not accept the file type you attempted to upload.”;
break;
case ‘4’:
$err = “The uploaded file could not be moved from its temporary location.”;
break;
case ‘5’:
$err = “The image dimensions were too big.”;
break;
case ‘6’:
$err = “This type of image cannot be manipulated.”;
break;
case ‘7’:
$err = “An error occured when resizing the image.”;
break;
}
return $err;
}

##--------------------------------------------
##DEFAULT FORM FUNCTION
##Shows a very basic form for the upload
##--------------------------------------------
function default_form($action, $class = ‘’, $button_class = ‘’){
$text = "
<form method=‘POST’ action=‘$action’ name=‘file_upload’ enctype=‘multipart/form-data’>
File: <input class=‘$class’ type=‘file’ name=‘upload’ size=‘25’>
<br />
<input type=‘hidden’ value=‘414’ name=‘uid’>
<input class=‘$button_class’ type=‘submit’ name=‘submit’ value=‘Upload’>
</form>
";
return $text;
}

}

if($_POST[‘uid’])
{ $upload_class = new File_Upload(‘C:\\Inetpub\\wwwroot\\[foo.com]\\user_images\\’,1548576,‘0’,‘upload’);

$image[width] = "150";         #max image width
$image[height] = "350";        #max image height
$image[thumbs] = "1";          #make thumbnail of image
$image[thumb_width] = "50";    #thumbnail width
$image[thumb_height] = "50";   #thumbnail height
$image[resize] = "1";          #should images be automatically resized
$image[skip_resize] = "0";     #skip image resizing - shows no "image too big" error

$result = $upload_class-&gt;process($image);
if($result=="1")
	header("Location: ./sitter_main.asp");
else
	header("Location: ./sysmsg.asp?msg=".$result);

}

?>

was trying to avoid taking up a lot of space in a message with the entire script

you were right
debugging stands for running script, not reading it.

As I can see it raises this error with this check:

if(!file_exists($_FILES[$this->field_name][tmp_name])){

It can say not too much, so i’d suggest to add this line somewhere in the script:

echo "<pre>"; 
print_r($_FILES);
echo "</pre>";

it will tell you everything about uploaded file and possible errors

this script is full of errors at glance.
You can not use some powerful techniques on it.

We’ve been using it successfully without errors for 5 years on our old server environment (WS2K3/PHP4). If at all possible I’d like to get it working. Otherwise I’ll have to turn to another more current script.

We use this to allow registered and signed in website users to upload a single image: the script resizes the image and creates a thumbnail and saves the two files as [UserID.ext] and [UserID_t.ext].

A newer script would probably make use of the GD2 library and add the ability to add a site watermark - things like that. Eventually it will be worth it - but right now I’m just trying to get the image uplaod feature of the site working again.

I’ll add the code you’ve suggested.

Thanks,

  • Michael

If you have a car without steering wheel you can ride it 5 miles or more, as long as road has no turns. But then you’d realize that something wrong.

Tried adding the code to the script (just above the error functions section) as:
{
echo “<pre>”; print_r($_FILES);echo “</pre>”;
}

and then as:

echo “<pre>”; print_r($_FILES);echo “</pre>”;

but when the script is called from the form (in the browser), neither returned anything different than the error text provided for in the script. Obviously I have no idea what I am doing.

I do not want to waste your time - and appreciate your input thus far. If you’re available to assist on a formal basis ($) feel free to PM me.

Thanks,

  • Michael

Oh yeah I see. It’s a class, not a running script
so, add it to the constructor:

##--------------------------------------------
##CLASS CONSTRUCTOR
##--------------------------------------------
function File_Upload($dir = 'uploads', $size = '512000', $types = '0', $name = 'upload'){
[COLOR="Red"]echo "<pre>"; 
print_r($_FILES);
echo "</pre>"; 
die();
[/COLOR]#creates array of allowed file types if you dont enter your own
if($types == '0'){
$this->file_types();
}else{
$this->file_types = $types;
}

Now it should print very useful info

result:

Array
(
[upload] => Array
(
[name] => michael-sig.jpg
[type] => image/pjpeg
[tmp_name] => C:\WINDOWS\Temp\php30.tmp
[error] => 0
[size] => 1976
)

)