Page 1 of 1

Cannot upload file on fedora 14 webserver

Posted: Tue May 21, 2013 10:25 pm
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

Re: Cannot upload file on fedora 14 webserver

Posted: Tue May 21, 2013 10:48 pm
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.

Re: Cannot upload file on fedora 14 webserver

Posted: Thu May 23, 2013 2:04 am
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

Re: Cannot upload file on fedora 14 webserver

Posted: Fri May 24, 2013 4:51 pm
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

Re: Cannot upload file on fedora 14 webserver

Posted: Thu May 30, 2013 1:53 am
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.