0

I'm trying to create a script that would allow the user to upload images onto the server. I've implemented the script,though once I tried to upload an image, the script follows through but no image appears on the server. Any ideas?

PHP:

<?php
require_once("connect.php");
ini_set('display_errors', 'on'); 
error_reporting(E_ALL);

echo __LINE__;

$allowedExtensions = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["image"]["name"]));
print_r($_FILES['image']);
echo $extension;

if((($_FILES["image"]["type"] == "image/gif") || ($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"]=="image/pjpeg") || ($_FILES["image"]["type"] == "type/png")) && ($_FILES["image"]["size"] <= 102400) && in_array($extension, $allowedExtensions)) {

        echo __LINE__;
        if($_FILES["image"]["error"] > 0) {
                $fileUploadFail = true; 
        }
        else {
               chmod("uploadImages/", 0755);
               move_uploaded_file($_FILES["image"]["tmp_name"], "uploadImages/" . $_FILES["image"]["name"]);
        }

} else {
        $fileUploadFail = true;
        echo __LINE__;
}   
        $fileName = $_FILES["image"]["name"];
        chmod("uploadImages/", 0600);
        echo __LINE__;
        /*if($fileUploadFile) {
              header("Location: uploadArt.php");
        }
        else {
             $title = $_POST['title'];
             $description = $_POST['description'];
             mysql_query("INSERT INTO `Art`(`File Name`, `Description`, `uploadLocation`, `Index`) VALUES('$title', '$description', 'uploadImages/$fileName', '')");
              header("Location: viewArt.php");
        }*/
  ?>

Output I receive:

6Array ( [name] => comps_tech.png [type] => image/png [tmp_name] => /tmp/phpgQKnMJ [error] => 0 [size] => 661 ) png2529

HTML:

<form id = "uploadDesigns"  enctype="multipart/form-data" name="Upload" method="post" action="fileUpload.php" >
<label for="title">Enter name of design:</label><input type = "text" id = "title" name = "title" size="50"><br /><br />
<label for="image">Upload image:<br />(max 100KB)</label> <input type = "file" id = "image" size = "51" name = "image"><br /><br />
<label for="description">Description:</label> <textarea id = "description" name = "description" rows = "4" cols = "20"></textarea><br /><br />
<button type="submit">Submit Art</button>
</form>

Thanks!

Andrew
  • 3,501
  • 8
  • 35
  • 54

3 Answers3

3

You have

input type = "file" id = "image" size = "51" name = "image"

But are referencing

$_FILES["file"]["type"] 

The "file" in the $_FILES refers to the name of the file input. This is "image", not "file"


The complete the answer (after fixing other issues and debugging); the last error is that

$_FILES["image"]["type"] == "image/png"

needs to be added to the if statement.

Robbie
  • 17,605
  • 4
  • 35
  • 72
  • i changed it, still seems to be a problem? – Andrew Jul 26 '12 at 06:17
  • Next thing to check is permissions. You set uploadedimages to 600 (rw) but you need 700 (rwx) for a directory. For now, set permissions to 777 to get it to work, and then 775 (if that files, check which group you're running as) then 755 (if that fails, check the user you're running as). – Robbie Jul 26 '12 at 06:32
  • Oh - you also set permissions on "/uploadedImages/" (absolute reference, from root, THAT WILL PROBABLY NOT EXIST), but you upload to "uploadedImages/" which is a relative reference, i.e. from where you are. So chance permissions on the relative directory, not the absolute one. – Robbie Jul 26 '12 at 06:38
  • hmmm.. i tried all the permissions, seems none of them work. May just be Kodingen doesn't allow me to upload files through PHP? – Andrew Jul 26 '12 at 06:45
  • What errors do you get? add "ini_set('display_errors', 'on'); error_reporting(E_ALL);" near the top of your PHP file. Then add "echo __LINE__;" in various places to check the script runs through the flow you expect. Type "print_r($_FILES);" as well to check the file is upladed. You need some basic debugging - we can't be phsycic ;) – Robbie Jul 26 '12 at 06:50
  • ah, I added in the lines, yet nothing's being printed out? – Andrew Jul 27 '12 at 03:05
  • actually, I get this: Notice: Use of undefined constant LINE - assumed 'LINE' in /var/www/vhosts/andrewgu12.kodingen.com/httpdocs/tora/manage/fileUpload.php on line 8 LINE – Andrew Jul 27 '12 at 03:07
  • "echo __LINE__;", not "echo LINE;". Sorry. – Robbie Jul 27 '12 at 03:34
  • "echo _ _ LINE _ _ ;" (without spaces around the " _ _ LINE _ _ " bit, not "echo LINE;". Sorry, the comment field converts the _ into emphasis. See http://php.net/manual/en/language.constants.predefined.php for what I'm referring to. – Robbie Jul 27 '12 at 03:42
  • all right, I tried it seems as though the code never reaches inside the if statement – Andrew Jul 27 '12 at 05:15
  • Escellent. So now add "print_r($_FILES['image']);" and "echo $extension;" to see why the "if" is failing. If $_FILES['image'] does not exist, then it means the file upload is failing (need ot check why as it's a server thing - so let's hope that's not the case) – Robbie Jul 27 '12 at 05:25
  • I get this as my output: Array ( [name] => comps_tech.png [type] => image/png [tmp_name] => /tmp/phpGp00lM [error] => 0 [size] => 661 ) png – Andrew Jul 27 '12 at 05:27
  • I presume by the accepted answer (thanks!) you got it. $_FILES["image"]["type"] == "image/png" needs to be added to the if statement. That should sort you out. – Robbie Jul 27 '12 at 05:45
  • I added it in, but somehow it still doesn't upload. – Andrew Jul 27 '12 at 05:48
0

You need to give write permissions for uploadImages.

asprin
  • 9,579
  • 12
  • 66
  • 119
0

Ur file directory structure should b like this.

1) test.php 2) uploadImages/ (this folder needs to be created by u ).

Shaikh Azhar Alam
  • 173
  • 1
  • 3
  • 16