I am having trouble getting my file upload to work. Up to now it has been working good, I recently had to upload a backup version of my website to the online server, but now the upload script does not work and, I suspect it may be a file upload permissions issue.

I understand I may have to inherit the folder/file permissions and ownership from my local host to the host server, but I not sure how to do this.

I cannot upload PDF files and/or images from the admin section upload form, but I can upload the files and/or images to the uploads folder by using either Filezilla FTP or by using the Cpanel File Upload with success.

I do not get any error message in the site admin upload form when submitting the form data, and the file/s in question are not present in the uploads folder or images folder.

The host server uploads folder permissions have been set at 0755, and the files permissions are set at 0644 in cPanel.

Nothing happens, and up until now everything worked ok. I have checked the script but I am not sure if everything is correct or not. It appears to be happening on all file uploads from the site admin file upload form.

I need file and/or image upload options in the admin panel for the site admin users to operate.

Can someone help me please? See below for example files:

upload_file.php - (for pdf & doc uploads):

<?php 
    session_start();
    //Must go through login and be Admin    
    if(!$_SESSION['loggedIn']) {
        header('Location: index.php');
    }
    $title = 'Upload File'; 
    require_once('template/header.php');
    require_once('template/connect.php');
?><br /><br /><br /><br /> 

    <div id="uploadFile">
        <div id="uploadFiles">
                <?php 
                    $target = "uploads/"; 
                    $target = $target . basename( $_FILES['uploaded']['name']) ; 
                $ok=1; 


                //This is our size condition 
                if ($uploaded_size >2950000){ 
                    echo "Your file is too large.<br>"; 
                    $ok=0; 
                } 
                //This is our limit file type condition 
                if ($uploaded_type =="text/php"){ 
                    echo "No PHP files<br>"; 
                    $ok=0; 
                } 
                //Here we check that $ok was not set to 0 by an error 
                if ($ok==0){ 
                    echo "Sorry your file was not uploaded"; 
                } 
                //If everything is ok we try to upload it 
                else { 
                    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
                    echo "&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
                          &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 
                          The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
                    } 
                    // else { 
                    //    echo "Sorry, there was a problem uploading your file."; 
                    //} 
                } 
            ?>
            </div>   
                <form enctype="multipart/form-data" action="upload_file.php" method="POST">
                    <fieldset>
                        <legend>&#160;Upload File&#160;|&#160;<a href="viewFile.php" class="apageLinks">View File List</a>&#160;|&#160;
                                                              <a href="menu.php" class="apageLinks">Main Menu</a>&#160;|&#160;
                                                              <a href="logout.php" class="apageLinks">Log out</a>&#160;
                        </legend><br /><br />
                        Please choose a file:<input name="uploaded" type="file" /><br /><br /> 
                        <input name="submit" type="submit" value="Submit" class="form" /><br /> 
                    </fieldset>       
                </form><br /><br /><br /><br /><br />
    </div>
<!-- End of page content -->
</div>
</body>
</html>                                                                                                                              It is a simple file upload script that I use to upload PDF files to the server. the files should be uploaded to the uploads folder in the admin section of the website, and these files are generally small in size, in fact I also use a similar script to upload images to the host sevrer.

Dani AI

Generated

Brief summary: the symptom (no error, file never appears) usually comes from two things working together — a bug in the upload script that suppresses or never reports errors, and a server-side ownership/permission or PHP-config restriction. ’s script shows a few silent-failure bugs; and are right to check ownership, and ’s 777 test can show a permissions problem but is not a permanent solution.

Immediate script checks (do these first):

  • Confirm the form field name and the keys used in PHP match exactly; the code shows an inconsistent index used in the success message.
  • Explicitly inspect $_FILES and its error code so failures aren’t swallowed.
  • Ensure the destination path is the real uploads folder (use an absolute path or DIR-based path) and that the directory exists.
  • Turn on temporary error reporting and log move_uploaded_file failures so the real reason appears in logs.

A minimal debugging snippet (illustrative) to add temporarily:

if (!isset($_FILES['uploaded'])) die('No file field');
if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) die('Upload error: ' . $_FILES['uploaded']['error']);
$dest = __DIR__ . '/uploads/';
if (!is_writable($dest)) die('Destination not writable: ' . $dest);
if (!move_uploaded_file($_FILES['uploaded']['tmp_name'], $dest . basename($_FILES['uploaded']['name']))) {
  error_log('move_uploaded_file failed: ' . $dest);
  die('Move failed — check permissions/open_basedir and error log.');
}
echo 'Uploaded';

Server/permission notes: 755 for directories and 644 for files is correct when the directory owner matches the PHP process user. Avoid 777 in production — use it only briefly to confirm a permissions problem, then revert. If PHP/Apache runs as a different user (or shows “nobody”), ask the host to set the uploads directory owner to the site user or enable a handler that runs PHP as the account user (suPHP/suEXEC). Also check php.ini values (file_uploads, upload_max_filesize, post_max_size), open_basedir restrictions and the PHP/Apache error log to pinpoint permission or configuration failures.

Recommended Answers

All 7 Replies

Hello,

This is going to be due to the settings in Apache that CPanel sets up. Is your new server running CPanel also if not it gets trickier. If it is running CPanel then you should set the user and group o the user id that owns the web site. If you can run ls -la /home/<your user> from a ssh connection and post the results I can give you an idea of the command to use to get it to work. For the time being you could do a chmod 775 on the upload directory so that the group can write and it might work. Need to see the actual listings to get a good idea.

Hi - I've encountered something similar before and it was beyond frustrating especially when I didn't get an error aswell.

Someone pointed out that it was indeed the file permissions and I was using FileZilla at the time. So I changed the folder's permission to where I'm uploading to 777 and it all worked 100%. Maybe this can work for you.

Hi Phillamon, yes I know about using 777 but by using that permission wouldn't it be too unsecure.

I only want the site administrator and Authenticated Users to have write permisssions to upload files (pdf, doc and images) to the server, and not everyone which is why I had set the permissions to 755, and at present, even that doesn't work.

It's frustrating, as this problem appeared out of the blue and I am having a lot of dificulty know how to fix it.

Cheers, davBro

check if folder permission is 755
and check Apache user which in Ubuntu is www-data
to find out Apache user
ps aux | grep apache
and do this command
chown -R www-data /path/to/folder

Howdy OsaMasw,

The folder permission is 755 and the fle permissions are 644.

I checked to see who the Apache User is, and it is listed as Nobody.

I do not understand this, it's getting a bit complicated for me how would I change from Nobody to an admin User (with full permissions) to enable file and image uploads for whichever admin user is uploading the file/image?

I now realise why I cannot upload files and/or images, but I do not know how to fix it, or inherit the localhost permissions to the host server?

Have you a fix for this please?

type
ls -l /path/of/folder
you should see the user who can manage this folder, if its not Nobody
then
chown -R Nobody /path/to/folder

Thanks OsaMasw, I'll give that a go and will let you know how it goes.

Cheers, davBro

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.