#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Code to upload image to directory


    Hi,

    this is the program i have for uploading a file to a directory specified. i have the client program in java which asks for the path and filename to upload. the uploading works from the java end. the server side is php program that gets that file and stores it in a directory called uploads. im not able to see the image there. i get the upload success message though.
    heres the code...

    <?php
    error_reporting(E_ALL | E_STRICT);

    $filename=$_GET['fileName'];
    $fileData=file_get_contents('php://input');
    $fhandle=fopen("./uploads/".$filename, 'wb');
    fwrite($fhandle, $fileData);
    fclose($fhandle);
    echo("Done uploading");
    ?>

    my logs say theres a problem with the fwrite parameter(has to be resource boolean...)...im not able to figure out how to debug this....any help is appreciated.thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    1) Please enclose your code in [ PHP ] tags. See the sticky at the top of this forum that says READ THIS BEFORE POSTING.

    2) The error indicates the fopen failed. In PHP I don't think 'wb' is valid. Try using 'w' as you don't need to distinguish a binary write.

    P.S. Why are you not using move_uploaded_file?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    this code below has the function u mentioned...i still have the same problem...im trying to figure out the permissions in the directory and how to set them meanwhile...

    <?php

    // Check for errors
    if($_FILES['file_upload']['error'] > 0){
    die('An error ocurred when uploading.');
    }

    // Check if the file exists
    if(file_exists('uploads/' . $_FILES['file_upload']['name'])){
    die('File with that name already exists.');
    }

    // Upload file
    if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], 'uploads/' . $_FILES['file_upload']['name'])){
    die('Error uploading file - check destination is writeable.');
    }

    die('File uploaded successfully.');

    ?>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    i also tried it with 'w' insted of 'wb'...no luck :-(
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    Are you sure you are referencing the correct file path? You are using a relative path so it is looking for that directory in whatever directory contains the PHP script. If the path is correct then what user/group owns that directory and what user/group is running httpd? If the user's are different then you may need to either change ownership or add one or the other to the group.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0
    it worked!!! like u said, it didnt work because the php file was in the same directory as the one in which i was trying to upload the image. i created a different directory and it worked!! thanks a million for helping me out! :-)

IMN logo majestic logo threadwatch logo seochat tools logo