Solved

Problem uploading files with PHP

Posted on 2012-03-19
6
333 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 110

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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

624 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