Cannot upload file on fedora 14 webserver

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
exbombo
Forum Newbie
Posts: 3
Joined: Tue May 21, 2013 9:31 pm

Cannot upload file on fedora 14 webserver

Post by exbombo »

Please help, I tried to upload file in my fedora webserver, but an error occured while the file is being uploaded. below is the error message:

Code: Select all

Warning: copy(read.txt): failed to open stream: No such file or directory in /var/www/html/eval/uploader.php on line 6 Could not copy file!
and below is my php code:

Code: Select all

//form for browsing the file
<html>
      <head>
              <title>File Uploading Form</title>
       </head>
<body>
             <h3>File Upload:</h3> Select a file to upload: <br />
             <form action="uploader.php" method="post" enctype="multipart/form-data">
                          <input type="file" name="file" size="50" />
                          <br />
                          <input type="submit" value="Upload File" />
              </form>
</body>
</html>

//php code to copy file into my folder eval

<?php
        if( $_FILES['file']['name'] != "" )
        {
               copy( $_FILES['file']['name'], "/var/www/html/eval" ) or 
               die( "Could not copy file!");
        }
       else
       {
               die("No file specified!");
       }
?>
please help how to fix the problem. Thanks in advanced
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Cannot upload file on fedora 14 webserver

Post by requinix »

First you have to fix your upload code to handle things correctly.

1. Check the error code first. There's a constant you can use to tell if the upload was successful.
2. If it's successful you need to validate. What kind of files do you want to allow?
3. If it's successful and valid then you can move the file to its new location. There is a specific function you should use to do this and it is not copy() or move().

Once that's ready you can fix the very minor problem you have now.
exbombo
Forum Newbie
Posts: 3
Joined: Tue May 21, 2013 9:31 pm

Re: Cannot upload file on fedora 14 webserver

Post by exbombo »

I already fix my code and it works perfectly in IIS webserver, however in fedora webserver i got the following error:
Warning: move_uploaded_file(upload/logo_img002.jpg): failed to open stream: Permission denied in /var/www/html/grd/upload_file.php

below is my code:

Code: Select all

<?php
if(!isset($_SESSION)){

	session_start();

}

$type = $_POST['type'];
$allowedExts = array("jpg", "jpeg", "gif", "png","JPG", "JPEG", "GIF", "PNG","doc","docx","pdf");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/doc")
|| ($_FILES["file"]["type"] == "image/docx")
|| ($_FILES["file"]["type"] == "image/pdf") || true) && ($_FILES["file"]["size"] < 1000000)

&& in_array($extension, $allowedExts))

  {

  if ($_FILES["file"]["error"] > 0)

    {

    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";

    }

  else

    {

	$newname = $type.'_'.$_FILES["file"]["name"];

	if($type=="logo"){		

		move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $newname);		

		echo "School official logo was uploaded successfully.";

	}

    }

  }

else

  {

	echo "Invalid file";

  }

  require("uploadForm.html");

?>

Please help what is really the problem, i am a beginner in php.
Thanks
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Cannot upload file on fedora 14 webserver

Post by mecha_godzilla »

Hi,

This is an issue with the folder permissions on your server - Apache has account privileges like every other user on the system, and your script is asking it to access a file that it does not have permission to access in some way.

What folder permissions are set for your "upload" folder? Remember that this is a two stage process - your script is telling PHP that it wants to read a file in the temporary upload folder (the location of which is specified in the php.ini settings) that PHP uses when it receives any files as a result of a form submission, and move this file to your "upload" folder. Normally, the temporary directory will have world-writable permissions (777) and is created automatically when you install PHP on a server, so the permissions for your "upload" folder are probably the reason why the move_uploaded_file() operation isn't working at the moment. Even if the permissions for your "upload" folder are correct (you would normally expect to see 755 or 775) the folder must also be owned either by the "apache" (or sometimes "nobody") user or owned by a user group to which the "apache" user belongs (this user group is also often called "apache").

HTH,

Mecha Godzilla
exbombo
Forum Newbie
Posts: 3
Joined: Tue May 21, 2013 9:31 pm

Re: Cannot upload file on fedora 14 webserver

Post by exbombo »

Thank you very much mecha_godzilla for your great analysis, you are precisely right, the problem is the permission of my upload folder. I changed my upload folder permission and ownership, now my upload script is working properly. Thanks.
Post Reply