PHP File Upload

PHP File Upload

Posted by: toby
Posted on: 2004-12-29 17:52:00

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?


Re: PHP File Upload

Posted by: macmanx
Posted on: 2004-12-29 18:20:00

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

Re: PHP File Upload

Posted by: user919
Posted on: 2004-12-29 23:25:00

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

jason

Re: PHP File Upload

Posted by: mobycat
Posted on: 2005-01-04 13:35:00

How do you run it as cgi?

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

Re: PHP File Upload

Posted by: macmanx
Posted on: 2005-01-04 14:05:00

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

Re: PHP File Upload

Posted by: user919
Posted on: 2005-01-04 14:29:00

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

jason

Re: PHP File Upload

Posted by: Logical Program
Posted on: 2005-01-06 22:42:00

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...

Edited by Logical Program on 01/06/05 10:43 PM (server time).

Re: PHP File Upload

Posted by: toby
Posted on: 2005-01-14 21:48:00

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.


Re: PHP File Upload

Posted by: snokarver
Posted on: 2005-01-15 17:10:00

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

Tags: php file uploaddreamhostadmin areatemp directoryuploadedhttptest serverwrite accesschmoduploadsftpaim