Forum Moderators: coopster

Message Too Old, No Replies

Problem uploading images to server

php file upload problem

         

jaminbeatty

11:09 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



I recently switched servers. I had a script I was using that would upload an image to a file on the server and store the image details into the db. After moving the site over, this script was no longer working. Initially I thought that all I would need to do was replace the directory path to the current directory path. I have used this same script on a couple different sites, each on different servers and this was all that was required to make it work.

I guess my question is this; are there any settings in the php.ini file I should be looking for?

I have tried about 8 different php file upload scripts both on the server I am having trouble with, as well as a test server I have set up. All the scripts work on my demo server, but all fail on the new server.

That's what is leading me to believe there is some kind of php setting that I am overlooking or something.

As I said before I know the script works, I have used it multiple times, but here is the code in case someone sees something that I may be missing:

<?php
$uploadDir = '/path/to/my/new/directory/';
if(isset($_POST['upload']))
{

$planid=$_POST['planid'];
$title=$_POST['title'];
$position=$_POST['position'];

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;
$position=$_POST['position'];
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file ";

exit;
}

include '../library/config.php';
include '../library/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (planid, title, position, name, size, type, path ) ".
"VALUES ('$planid', '$title', '$position', '$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include '../library/closedb.php';

echo "<br>Files uploaded<br>";

}
?>

Any help or further insight is hugely appreciated.

sonjay

11:30 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Do you get any error messages? A blank screen? Or what?

rocknbil

12:25 am on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard jaminbeatty,

$uploadDir = '/path/to/my/new/directory/';

First on the list: verify the directory is writable. Check permissions there.

Second stop: paste this at the top of your script, but be SURE to remove it or comment it out before going live. To-the-screen error reporting is off by default on most servers as a security precaution.

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

Nothing helpful? This means it's erroring before error_reporting can get to it. So . . . .

Third stop: error logs. If there's an error, it will show here. SSH in (or however you read error logs,) attempt an upload, then in your CLI immediately enter

tail /var/www/yourdomain.com/stats/wherever/your/error_log

This may give you a clue.

Last two things, probably not helpful but worth looking at: you can check your PHP versions, but I don't see anything there that would cause it to die in 4 or 5. The other is to make sure you're uploading files under 2MB, this is the default upload for PHP. If you need to upload larger files, you will have to tweak the system-wide .ini or add values to an .htaccess file in the directory of your scripts to increase max_upload_size, timeout values, and memory usage.

jaminbeatty

5:52 pm on Mar 3, 2009 (gmt 0)

10+ Year Member



Thank you both for your replies sonjay and rocknbil.

1. Yes, the file directory is writable. It is chmod'd to 777.

2. I added the following lines

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

to the beginning of the script was still unable to see any error messages coming up.

3. So as you suggested, I ran the upload script the immediately SSH'd in to view the error log. Here is the error that I received:

(32)Broken pipe: client stopped connection before mmap completed

Any ideas on where to go from here?

jaminbeatty

7:22 pm on Mar 5, 2009 (gmt 0)

10+ Year Member



If anyone cares, this issue has been resolved. After exhausting all possibilities that it could be on my end, or with the script, it turns out it was a configuration setting in the php.ini file. 2 days ago I put in a support ticket with my hosting provider and they were able to correct the problem. The temp directory for uploads was never set in the ini file.

So to anyone who has a similar problem and comes across ths post, check your ini file.

Thanks Rocknbil for your input though. Your suggestions did help me further narrow down the problem so that I knew what to discuss w/ my hosting provider.