PHP File Upload

software development

#1

My aim is to build a website with PHP that provides an admin area where photo (JPG) files can be uploaded via a HTTP form into the website along with data that will be stored in a database. I don’t want the user of the admin area to have to resort to FTP to upload the picture files.

I got all of this working on my own test server at home, but when I uploaded the code to DreamHost, the HTTP form file upload facility did not work. I tried chmod on the file upload directory, but that did not make any difference. I think that DreamHost have configured a temp directory in PHP for uploads, then denied write access to it.

Does anyone know whether HTTP form file upload with PHP can be achieved on the DreamHost web servers?


#2

Yes, it can. WordPress has a similar function and it works just fine.


MacManX.com


#3

have you made sure that php is running as cgi in addition to the permission issue?

[color=#0000CC]jason[/color]


#4

How do you run it as cgi?

(I’m having the same issue, and Dreamhost has yet to contact me)


#5

KB Article: How do I switch my PHP between running as a CGI and an Apache module?


MacManX.com


#6

You can also do it by using the file extension PCGI instead of PHP on files, or use htaccess to do it directory specific, add the following to your htaccess:

AddType php-cgi .php

all directories below the one with that in it will run php as cgi.

Recap:
Globally: DH panel
Directory: htaccess
File: PCGI extension

[color=#0000CC]jason[/color]


#7

Try this… Should DEFINITELY WORK.

Use this code…

$uploaddir = dirname($_SERVER[‘SCRIPT_FILENAME’]).’/’;
$uploadfile = $uploaddir . basename($_FILES[‘FILE FIELD NAME’][‘name’]);
$file = $_FILES[’‘FILE FIELD NAME’][‘name’];
chmod("$uploadfile", 0777);

$uploaddir is set to the current directory, add additional directories after the forwardslash.
$uploadfile is the directory/filename.extension…
$file is the actual filename
After this, it will chmod the file that was just uploaded, so there should be no permissive errors…


#8

First I checked my PHP settings in DreamHost control panel. PHP was not set to run as CGI, so I changed it. I waited 30 minutes, then tried the PHP file upload script again, but the result was the same.

Next I tried renaming the script file extension to PCGI. This fixed the problem immediately.

A couple of hours later, I renamed the file back to PHP. The script still worked, so obviously changing PHP to run as CGI had the desired effect.

Thanks all of you for your help.


#9

One thing though, your script wont upload anything larger than 7MB.


#10

Trying to create a PHP file upload and nothing above is helping???