SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Saving images from a given URL to harddisk in my form

    Hi, i am very new in writing image handling script. I am starting to write a PHP script for my form such that the users will input an url which contain their avatar to my server harddisk...

    the form input name is called: avatar_link

    and following is my script for the action page:

    PHP Code:
    $uploaddir "avatarimages";

    if ((
    $imageavatar getimagesize($_POST["avatar_link"])) == true)
    {
         if(
    $imageavatar[2] == || $imageavatar[2] == 2//gif or jpeg
         
    {        
          
    $handle fopen($_POST["avatar_link"], "r");
          
    $contents fread($handle1000000);
            
          
    $handle1 fopen($uploaddir."/abc.gif""w");
          
    fwrite($handle1$contents);
          
    fclose($handle);
          
    fclose($handle1);
          }

    Problem 1: it seems to have some problem as the uploaded image only displays half a portion. It is definitely not a big image that i had uploaded because it's actually a small gif.

    Problem 2: I do not know how to write the pic file to the folder, there's why i use fopen and fclose. I am sure i am not doing the right thing...

    Please help... I am thinking of about giving a "Upload from local machine" feature and i think it's easier because i can use $_FILES unlike this situation.
    I LOVE PHP!!!

  2. #2
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe your issue is with
    PHP Code:
    $contents fread($handle1000000); 
    only reading 1000000 bytes of data.

    Try instead

    PHP Code:
     $uploaddir "avatarimages";

    if ((
    $imageavatar getimagesize($_POST["avatar_link"])) == true)
    {
         if(
    $imageavatar[2] == || $imageavatar[2] == 2//gif or jpeg
         
    {        
          
    $contents file_get_contents($_POST["avatar_link"]);
          
    $handle1 fopen($uploaddir."/abc.gif""w");
          
    fwrite($handle1$contents);
          
    fclose($handle);
          
    fclose($handle1);
          }

    and see if that helps.

    You will need url wrappers enabled in your php.ini but I am assuming it is already so since you are getting half an image

  3. #3
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks alot.
    i didn't know we have this file_get_contents function
    I LOVE PHP!!!

  4. #4
    SitePoint Zealot diskhub's Avatar
    Join Date
    Jun 2004
    Location
    singapore
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,

    is it possible to check the filesize of a image from another url?
    I have tried file_get_contents(URL);
    but i can't do a filesize in it...

    what shd i do?
    I LOVE PHP!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •