0

i need your help...i want to copy/upload images on server and saving directories in database...Problem is the images are empty =( I'm using:

  • WampServer
  • Apache Version : 2.2.22
  • PHP Version : 5.3.13
  • MySQL Version : 5.5.24

Issue is that it's creating the file but is empty.... can it be WampServer fault?

CODE:

<input type="hidden" name="MAX_FILE_SIZE" value="30000">
<input name="image" id="image" multiple="true" type="file" />
<input name="name" id="name" type="text" maxlength="50" value="" placeholder="Enter Image Name" class="text-field"/>


if(isset($_POST['name']) && isset($_POST['image']))
    {
        $name = $_POST['name'];
        $img = $_POST['image'];

        $file_url = $img;
        $fp = fopen($file_url, 'rb');
        $content = fread($fp, filesize($file_url));
        $fp = fopen('../Images/UploadedImages/'.$img, 'wb');
        $image='../Images/UploadedImages/'.$img;

        $result=mysql_query("insert into Images(Name,Directory,Register_Day) 
        values ('$name','$image',now())");

        if (!$result) {
            die("Failed to load");}
        else{
            fputs($fp, $content);
            fclose($fp);
            }
4

2 回答 2

1

Uloading images doesn't work like that, you must do something like this:

$img = $_FILES['image']['tmp_name'];

Also note that you don't want to save the image into the database, you (probably) want to save the image to the server but you save only the file location into the database.

于 2013-05-05T19:24:22.913 回答
0

You cant upload image by using fopen. Try below code. Your will get image details in $_FILES array not in $_POST.

$tmp_name = $_FILES['image']['tmp_name'];
$name = $_FILES['image']["name"];
$uploads_dir = '../Images/UploadedImages';
move_uploaded_file($tmp_name, "$uploads_dir/$name");

In above code $uploads_dir/$name will be your image path which you can store in db.
And also make sure that your <form> tag has enctype="multipart/form-data" attribute set.

于 2013-05-05T19:38:04.540 回答