Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem uploading files with PHP

Posted on 2012-03-19
6
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

715 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