Solved

Problem uploading files with PHP

Posted on 2012-03-19
6
326 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 21

Expert Comment

by:Kim Walker
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:dmgroom
Comment Utility
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
Comment Utility
@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
Comment Utility
Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now