8 Replies - 560 Views - Last Post: 17 June 2012 - 12:49 PM Rate Topic: -----

#1 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Uploading image

Posted 17 June 2012 - 10:42 AM

Hello all,

i want to upload an image to my server using XAMPP, but an error keeps appearing.
My code is the following :

<?php
$con = mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("photoshare", $con);


if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("photo_album/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "photo_album/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "photo_album/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>




and the error is the following :

Upload: 6728_1187872251165_1059072917_617969_161285_n.jpg
Type: image/jpeg
Size: 6.9443359375 Kb
Temp file: C:\xampp\tmp\php30E9.tmp

Warning: move_uploaded_file(photo_album/6728_1187872251165_1059072917_617969_161285_n.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\retalis\uploadImage.php on line 35

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php30E9.tmp' to 'photo_album/6728_1187872251165_1059072917_617969_161285_n.jpg' in C:\xampp\htdocs\retalis\uploadImage.php on line 35
Stored in: photo_album/6728_1187872251165_1059072917_617969_161285_n.jpg


any help would be appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Uploading image

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,215
  • Joined: 23-August 08

Re: Uploading image

Posted 17 June 2012 - 11:13 AM

Go read this carefully, please.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Uploading image

Posted 17 June 2012 - 11:19 AM

Also note the actual error message: "failed to open stream: No such file or directory"

Does the "C:\xampp\htdocs\retalis\photo_album\" directory actually exist? And does PHP have write access to it?
Was This Post Helpful? 0
  • +
  • -

#4 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: Uploading image

Posted 17 June 2012 - 11:39 AM

Yes path exists, idont know about access though.
Its xampp files so why not ahveing access?

also jack i tried to fixe size, i renamed directory to photoalbum (from photo_album), i tried change $_FILES["file"]["name"]; to $HTTP_POST_FILES. Nothing worked

i am pretty sure its a folder problem i will keep searching your link though

thanks for your patience
Was This Post Helpful? 0
  • +
  • -

#5 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: Uploading image

Posted 17 June 2012 - 11:55 AM

also i noticed that this error appears only in one specific image
to all the other images another error appears (invalid file)

in both cases i use .jpg files
in my code i have permission for jpeg, pjpeg and gif

isnt jpg and jpeg the same?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Uploading image

Posted 17 June 2012 - 11:56 AM

You can easily figure out about the access. PHP has functions that can determine what kind of access it has. Try putting this at the very top of the file, just for debugging purposes.
$uploadPath = "photoalbum/";

if (!file_exists($uploadPath)) {
	echo "Upload dir doesn't exist!";
}
else if (!is_readable($uploadPath)) {
	echo "Upload dir can't be read!";
}
else if (!is_writable($uploadPath)) {
	echo "Upload dir can't be written to!";
}
else {
	echo "Upload dir is ready and waiting!";
}
exit;


It'll tell you whether or not you have access.


View Postdemoloution, on 17 June 2012 - 06:39 PM, said:

... i tried change $_FILES["file"]["name"]; to $HTTP_POST_FILES....

No, don't do that! $HTTP_POST_FILES is not used anymore. It exists only for backwards compatibility with PHP programs written back when mankind was still trying to figure out how to work fire...
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: Uploading image

Posted 17 June 2012 - 12:01 PM

View Postdemoloution, on 17 June 2012 - 06:55 PM, said:

isnt jpg and jpeg the same?

Yes, it should be. It's the same format, only the .jpg extension is still lingering from the days when MS-DOS restricted file extensions to three letters. Both should have the same mime-type when uploaded. (image/jpeg)

Try printing the file type as it is in the $_FILES array with the invalid image error, to see what you're actually getting.
Was This Post Helpful? 0
  • +
  • -

#8 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: Uploading image

Posted 17 June 2012 - 12:19 PM

Ok so heres the results :

first of all, i changed the "photoalbum/" to "../photoalbum/"

for my first image with the big error, the upload was successful
for all the other images (all of them are bigger in size) i keep having the "invalid file" error

also i tried your code atli and it throw me the "upload dir does not exist" error.But i run this after i changed to "../photoalbum/" so i guess thats why there was an error :P

so now i am thinking that maybe i havent changed the size for my uploading files properly. The images i tried to upload are 60-80Kb (pretty small) but still getting the error "invalid file"

i changed the size from "&& ($_FILES["file"]["size"] < 20000))"
to && ($_FILES["file"]["size"] < 200000))

i am not sure if adding zeros is the proper way though

thanks again for all your patience
Was This Post Helpful? 0
  • +
  • -

#9 demoloution  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 14-November 10

Re: Uploading image

Posted 17 June 2012 - 12:49 PM

problem solved
i should have changed the size earlier and with more accuracy..one 0 in the end dont make the difference :P
no need for php.ini files
so i change the && ($_FILES["file"]["size"] < 20000))
to && ($_FILES["file"]["size"] < 50000000))
and things are perfect now

thank you all for your replies :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1