开发者

upload image in php by its url

i have this form to upload images:

<form method="post" action="upld.php" name="insertForm"  enctype="multipart/form-data">
Image name:<br />
<input type="text" name="iname" /><br />
<input type="file" name="file" />
<input type="submit" name="upload" value="Upload" />

</form>

and here is the upld.php

<?php
$db_name = "DB_name";
$ta开发者_Python百科ble_name = "tble";
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db($db_name, $connection) or die(mysql_error());

if(isset($_POST['upload'])){

 if  (($_FILES["file"]["error"] > 0))
    {
echo "<h3> Error in File! </h3>";
    }
  else
    {
    if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "images/" . $_FILES["file"]["name"]);

//$id=mysql_insert_id();
$time=strftime("%Y-%m-%d %H:%M:%S", time());
$img_name=$_POST['iname'];
$img=$_FILES["file"]["name"];
$sql="INSERT INTO $table_name VALUES('{$img}','{$time}','{$img_name}')";
$result=mysql_query($sql,$connection);
mysql_close($connection);
echo "<h3>uploaded successfully</h3>";
}
}
}
echo "<br><br><a href='GalleryAdmin.php'>GO back to Admin Gallery</a>
";
?> 

the problem is:

when i run it always say me file not exsist, acording to this if

 if ((file_exists("images/" . $_FILES["file"]["name"])) )
      {
      echo "<h3> file not exsists!</h3>";

i have the images folder with upld.php in the same folder

what would you guess is the problem?


I think you have a slight logical error

file_exists("images/" . $_FILES["file"]["name"])

Will return true if the file exists in the images folder (my guess would me if somebody already uploaded it). But, based on your log statement, what you want is

!file_exists("images/" . $_FILES["file"]["name"])


OK so you uploaded your file. But, what you checked was if it was in say "images/my.jpg". At this point its in tmp_name, in your tmp directory, most likely so, no it would always not exist at this point as the file is only in a temporary name, you need to check its in your temp location, move it, and then check if its in images surely?


First:

PHP uploads the file to a temp directory. This is the file you need to move to your images/ folder. You find the file in this location on your server:

$_FILES['file']['tmp_name']

This is the file on which you want to run file_exists to make sure the upload completed successfully. So:

if (file_exists($_FILES['file']['tmp_name']) {
  // File upload successful. Now move file to your directory.
  move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]);
  // Now do the database stuff here.
  // ... 
} else {
  // Nothing was uploaded and something is wrong!
}

Secondary:

Your code

file_exists("images/" . $_FILES["file"]["name"])

will return TRUE, and therefore (in your code) it will say that there are no file. That's a logical error on your part.

Try:

!file_exists("images/" . $_FILES["file"]["name"])

instead.

Third:

Make sure that the file to which you move the file (images/) have the proper chmod. It needs 775 for it to be possible to create files into. This is made via the ftp program.

Read more here: CHMOD tutorial

You will also need to move the file from the tmp dir to images before checking if it's there with file_exists.


Please use move_uploaded_file before you check if the file exists;)

Otherwise try this:

1.) error_reporting(E_ALL);

2.) chmod the images directory (775), right mouse click on directory

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜