Solved

Problem uploading files with PHP

Posted on 2012-03-19
6
329 Views
Last Modified: 2012-04-18
I have used this php file upload script several times with no problems, but now that I have switched to a GoDaddy server I cannot seem to get it to work.  My form looks like this:

            <form enctype="multipart/form-data" action="upload.php"
            method="post">
            <input type="hidden" name="MAX_FILE_SIZE"
            value="20000000" />
            Title: <br /><input type="text" size="80" name="title" /><br />
            Description: <br /><textarea rows="5" cols="60" name="description" style="font-family:Arial, Helvetica,sans-serif" /></textarea><br />      
            Select a File:<br />
            <input type="file" size="20" name="thefile" /><br />
            <input type="submit" name="Submit" value="Submit" />
            </form>

And the file upload.php looks like this:

<?
include('.dbconnect.php');
include('login_checker.php');

// First we move the file to the directory
if(array_key_exists('thefile', $_FILES)) {

// Validate the uploaded file
if($_FILES['thefile']['size'] === 0
|| empty($_FILES['thefile']['tmp_name'])) {
    echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['thefile']['size'] > 20000000) {
    echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['thefile']['error'] !== UPLOAD_ERR_OK) {
    // There was a PHP error
    echo("<p>There was an error uploading.</p>\r\n");
} else {

// Create directory if necessary
if(!file_exists('uploads')) mkdir('uploads');

// Move the file
if(move_uploaded_file($_FILES['thefile']['tmp_name'],
'uploads/' . $_FILES['thefile']['name'])) {
} else {
    echo("<p>There was an error moving the file.</p>\r\n");
}
}
}

//Second we write information about the upload to the database

$title = $_POST['title'];
$description = $_POST['description'];
$file_size = $_FILES['thefile']['size'];
$file_name = $_FILES['thefile']['name'];
$file_type = $_FILES['thefile']['type'];

mysql_query("INSERT INTO audio (title, description, size, file_name, type)
VALUES ('$title', '$description', '$file_size', '$file_name', '$file_type')");

mysql_error();
?>

<?php
// The form isn't misbehavin then…
if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') {  
    $poidsMax = ini_get('post_max_size');
    $oElement->addError("fileoverload", "Your file is too big, maximum allowed size here is $poidsMax.");
}
?>

I am trying to upload audio files and the error that I keep getting is:  "No file was selected."
Which has to come from this part of the upload.php file:
if($_FILES['thefile']['size'] === 0
|| empty($_FILES['thefile']['tmp_name'])) {
    echo("<p>No file was selected.</p>\r\n");

The data is written to the database table, but the file is not moved to the directory.  I have looked at this piece of code for a long time and can't see an error in the form or the upload.php file so I think it might be my folder permissions or something else.

I am using Filezilla to upload my files and I couldn't reset the directory permissions with Filezilla, the permissions kept reverting to old settings.  So I switched to the GoDaddy control panel and I was able to set the directory that contains form.php and upload.php to "Read" and "Write"... "Execute" is not an option. I also set the "uploads" directory to "Read" and Write"... again, "Execute" is not an option.  

This is probably something simple I am overlooking, I appreciate any direction anyone can provide. Thanks.
0
Comment
Question by:jbpeake
6 Comments
 
LVL 32

Expert Comment

by:DrDamnit
ID: 37738124
but now that I have switched to a GoDaddy server I cannot seem to get it to work
... And therein lies your problem.

If it worked before, and now that it doesn't work on Godaddy's system, then it is a godaddy settings problem.

Don't change your working code to fit the server, contact GoDaddy support and have them change the server to accomodate your code.

If they cannot, then do what I do: use bluehost.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 37738512
I use and am pleased to recommend Chihost.com.  I have also heard good things about Bluehost.  Good experiences also with HostGator, DreamHost, and LiquidWeb.

You might want to run phpinfo() on the old and new hosts and compare the output line-by-line.  You may be able to spot the difference.

Sidebar note to DrDamnit: have you no shame!  Telling a member of EE to contact GoDaddy support!  Yecch!

 ;-)

Best to all, ~Ray
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37739457
Create a php file which contains a single command as follows:
<?php phpinfo(); ?>

Open in new window

Upload this file to your server and then load this page in your browser. Search for two values -- one for post_max_size and one for upload_max_filesize. These may be preventing your file from being processed. Be sure to remove this file from your server after use. It contains information you don't want everyone to know.

Oddly enough, the file file size may not be determined until after the upload at which point it is discarded if it's too big. See the following PHP reference to help with max file size:
http://us2.php.net/features.file-upload
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Expert Comment

by:dmgroom
ID: 37742883
1) How large a file are you trying to upload?  The max file size in your form is set to 20Mb, but this may be much larger than the  size set in php.ini.

Have you tried uploading a small file, say a few hundred Kb in size?

2) Im surprised to see the lines
// Create directory if necessary
if(!file_exists('uploads')) mkdir('uploads');

Open in new window

If you need the uploads directory why not just create the directory manually and set its permissions .

3)  In  
// Move the file
if(move_uploaded_file($_FILES['thefile']['tmp_name'],
'uploads/' . $_FILES['thefile']['name'])) {

Open in new window

 try replacing 'uploads/'  with the aboslute path to the uploads directory
0
 
LVL 32

Accepted Solution

by:
DrDamnit earned 500 total points
ID: 37750567
@Ray:

I have NO SHAME when it comes to GoDaddy and their servers. Last time I checked, they were a full MAJOR version of PHP behind what is generally used. (They were still on PHP 4 not long ago). They have some silly / wired configs over there too.

Very rarely will I tell a fellow EE'er to contact support... but I have found that with GoDaddy... it is usually a shortcut to save a lot of headaches. After support confirms that there is nothing "stupid" going on on their side, then we can all jump in.

Of course, that was before the MIGHTY and MASTERFUL <angelic_music> ray_pasuer</angelic_music> jumped in. I bow before thee so the weight of your 9,000,000 PHP points does not squash me like a bug. :-)

Hope you're doing well. We haven't collaborated on a question in a while. I always look forward to your elegant solutions.
0
 

Author Closing Comment

by:jbpeake
ID: 37864159
Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question