SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Uploading files.....

    I am trying to write a small program that will allow users to upload files to my server. I have been able to accomplish this if the file is stored on the user's local system. But I have not been able to get this to work if the file is stored somewhere on the web.

    Does anyone have a code snippet that accomplishes this? I have looked at the php manual, but it doesn't seem to cover this action.

    Thanks

    CDitty

  2. #2
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I forgot I need a code for that too!, Although I need one that will get a file off a users computer, and change the filename to the user id number. Then save that file on my server.

  3. #3
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a great tutorial on phpBuilder concerning that.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  4. #4
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post a url for this tutorial? The only thing I could find is one that only lets the user upload one stored on their system. I need one that will download it from another website.

    Thanks

    Chris

  5. #5
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh.. download from another website... ouch.

    I'm not sure, mabey try getting the file then checking if you got anything... I'm just not sure...
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  6. #6
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't really think it would be that hard. Should be anyway.

    Thanks for trying.

    Any one else?

  7. #7
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $fp = @fopen("http://www.domain.com/pic.jpg","r"); 
    if (
    $fp){
        echo(
    "The file exists!)";
    } else {
        echo(
    "The file does not exist");

    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  8. #8
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ws just looking at that, it's easy to understand, but here is a much better functional use of error checking:

    PHP Code:
    if($fp = @fopen("http://www.domain.com/pic.jpg","r")){
        
    // do stuff
    } else {
        
    // display error
        
    echo("File does not exist.");

    Don't forget to supress the error with the @ sign... if you don't you will will have some weird errors come back to the user... and we wouldn't want that.
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  9. #9
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    PHP does have bulit-in ftp functions, there was another topic about using them recently.

    Sean

  11. #11
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the coding exbabylon, I'll give it a try.

    zoordaan, please see my reply 4 messages up. That article is only for uploading from the user's local machine. I need to be able to pull from another website.

    seanf, thanks, but I can't do this with FTP. Must be HTTP only.

    Thanks all

    Chris

  12. #12
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the php manual page on fopen, read the notes posted by users, one of them has something that you might be able to use. http://www.php.net/manual/en/function.fopen.php

  13. #13
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have looked there several times and can only find references to reading txt files and html file. Unless I missed it, there isn't anything there pertaining to graphic files.

    Using the example exbabylon gave and some sinppets I have, this is my code so far. Only problem is I keep getting
    "Warning: Unable to open '/tmp/tab2-3x5.gif' for reading: No such file or directory in /mypath/url.php on line 28
    "

    Any ideas?

    PHP Code:
    <?
    $userid 
    "cditty";
    $url "http://www.mydomain.com/images/tab2-3x5.gif";
    if(
    $fp = @fopen($url,"r")){
        
    // do stuff
        
    $arr_basename explode("/"$url);
        echo 
    $arr_basename[4] . "<br>";
        
        
    // change $final location to be your final storage place for uploaded file
        
    $tmp_upload_path "/tmp/"// Temporary location of file
        
    $final_location="/mypath/$userid/";
        
        
    // extract file extension of uploaded file
        
    $arr_basename explode("."$arr_basename[4]);
        
    $file_name $arr_basename[0];
        
    $file_name ereg_replace(" ""%20"$file_name); 
        
    $file_type $arr_basename[1];
        
        echo 
    $file_name "<br>";
        echo 
    $file_type "<br>";
        
        
    $new_file_name $file_name "." $file_type;
        
    $temp_full_path "/tmp/$new_file_name"// <-----Error line
        
    $final_full_path $final_location $new_file_name;
        
        echo 
    $final_full_path "<br>";
        
        if(!
    copy($temp_full_path$final_full_path)){
            echo 
    "Failed to upload $file...\n<br>Please try again.<br>";
        }


    } else {
        
    // display error
        
    echo("File does not exist.");
    }
    ?>

  14. #14
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This was added by maxxmaster@nrj.com on 18-Jan-2001 12:10

    You would have to test it to see if it works, but this seems to be what you want to do.

    I've tryed to copy a file located somewhere in the web to a local destination; It works quite well, but is there a faster method for that??

    Here's my example:
    Code:
    $handle = fopen ($WebFile, "r"); 
    $outhandle=fopen ($LocalFile,"w"); 
    while (!feof($handle)) { 
    $buffer=fread($handle,4096); 
    fputs($outhandle,$buffer); 
    } 
    fclose($handle); 
    fclose($outhandle);

  15. #15
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by zoordaan
    File uploads made easy - http://www.phpbuilder.com/columns/bealers20000904.php3

    On this article it says to use the address of the site on the server.

    "/usr/local/apache/htdocs/sites/dev/phpbuilder/upload/files"

    Thats the address it use's....

    Our head tech guy is gone for the day. Is there anyway I can find out the address without having to spend 4 hours trying to get ahold of him just to ask him that?

  16. #16
    SitePoint Addict zoordaan's Avatar
    Join Date
    Feb 2001
    Location
    NYC/Texas
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you can get this address from your ftp program.

  17. #17
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    manipura,

    Make a file called phpinfo.php, and place it on your server in the dir you are trying to get, it should contain the following code:

    PHP Code:
    <font face="verdana,arial" size="2"><b>This is all the information about PHP. Scroll down to <i>Apache Environment</i>, then look at <i>Script Name</i>. That will be your correct server path.
    <?php
    phpinfo
    ();
    ?>
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services

  18. #18
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got the upload working correctly. Thanks to zoordaan for showing me that little code snippet. That was the key.

    I now need to get the filesize for the file uploaded. But for some reason, it always returns 0 bytes. I can get a firectory listing but can't get the filesize.

    Does anyone know if there is a problem/bug with the filesize and version 4.0.4pl1?

    here is my code.
    PHP Code:
    <?
    $new_file_name 
    "cditty.jpg";
    $userid "cditty";
    $handle opendir("/home/$userid");
    while(
    false!==($file readdir($handle))){
        if(
    $file == "cditty.jpg"){
             echo 
    $file " " filesize($file) . "<br>";
        }
    }
    ?>
    Chris

  19. #19
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $filesize = filesize("$path/$filename");

    $filesize contains the filesize of $filename in bytes. =)
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  20. #20
    SitePoint Zealot CDitty's Avatar
    Join Date
    Feb 2000
    Location
    Memphis Tn
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. Believe it or not, I tried that. Didn't work last night, but it did this am. (Maybe I was just too tired.)

    Anyhow, I am trying to use this when the user uploads an image from the web. It simply refuses to work. It always reports the filesize as 0 bytes even if the actual size is correct. If I use the same directory listing code on a seperate page, it works fine. I have even tried doing a sleep() and a clearstatcache().

    Thanks

    Chris


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
  •