954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Remote file upload help needed

This is my normal file upload code.

HTML

<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
          <input name="theFile" type="file" />
          <input name="Submit" type="submit" value="Upload">
    	</form>


PHP Variables to receive file

$fileName = $_FILES['theFile']['name'];
    $fileTempName = $_FILES['theFile']['tmp_name'];


Now i would like to use remote upload.

So i created a form like this

<form method="POST" action="<?=$self?>">
    <input type="text" name="file[]"  size="45" value="">
    <input name="submit" type="submit" id="submit" value="submit" accesskey="s"></p>
    </form>

I have to enter file url in the above form. When i submit the form i want the file file details store in these variables$fileName,$fileTempName

I don't want them to store locally. I'm trying to use those variables in amazon s3 upload. Could you guys help me?. Thanks

Viruthagiri
Light Poster
38 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

You can use pathinfo() to get the information: http://php.net/manual/en/function.pathinfo.php

<?php
    $path_parts = pathinfo('http://remote.website/images/test.jpg');
    echo $path_parts['dirname'], "\n";
    echo $path_parts['basename'], "\n";
    echo $path_parts['extension'], "\n";
    echo $path_parts['filename'], "\n";
?>


but you still don't have a TempName because you're not uploading a file, if you need it, you should generate that on your own when youdownload the file to the server, here's a simple example:

<?php

    $url = 'http://remote.website/images/test.jpg'; # $_POST['link']
    $getFile = pathinfo($url);
    
    $fileName = $getFile['filename']; # without extension
    $fileTempName = sha1(microtime().uniqid('',true)); # random name
    
    $image = file_get_contents($url);
    $wr  = fopen('/var/www/path/images/'.$fileTempName.'.jpg', 'w+');
    fputs($wr, $image);
    fclose($wr);
    
    unset($image);
?>


This will download and rename the image to the images/ directory, or wherever you want, in order to upload to Amazon S3. You should check at least if file exists, and if the file is really an image.

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

You can use pathinfo() to get the information: http://php.net/manual/en/function.pathinfo.php

<?php
    $path_parts = pathinfo('http://remote.website/images/test.jpg');
    echo $path_parts['dirname'], "\n";
    echo $path_parts['basename'], "\n";
    echo $path_parts['extension'], "\n";
    echo $path_parts['filename'], "\n";
?>

but you still don't have a TempName because you're not uploading a file, if you need it, you should generate that on your own when youdownload the file to the server, here's a simple example:

<?php

    $url = 'http://remote.website/images/test.jpg'; # $_POST['link']
    $getFile = pathinfo($url);
    
    $fileName = $getFile['filename']; # without extension
    $fileTempName = sha1(microtime().uniqid('',true)); # random name
    
    $image = file_get_contents($url);
    $wr  = fopen('/var/www/path/images/'.$fileTempName.'.jpg', 'w+');
    fputs($wr, $image);
    fclose($wr);
    
    unset($image);
?>

This will download and rename the image to the images/ directory, or wherever you want, in order to upload to Amazon S3. You should check at least if file exists, and if the file is really an image.

Hi thanks. But i'm getting error messages. I'm using the code explained in this tutorial. http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/

This is the error message i'm getting.

Warning: S3::inputFile(): Unable to open input file: 4e4aca9e87af7612683548f6edb5267790c92aee in public_html/amazons3/S3.php on line 222
Something went wrong while uploading your file... sorry.

I uploaded that amazon S3.php class in pastebin. This is the file.
http://pastebin.com/Wx5QtXDs

Thanks

Viruthagiri
Light Poster
38 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

I can't help you with Amazon but the error seems to be simple, $fileName is only a random string, you need to set the fullname and also the path, so you need to add something like this:

$basename = '/var/www/test/images/'.$fileName.'.jpg';


Otherwise the script doesn't know which file is to be open and where to look. I hope this helps you :)

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

Ok thanks for the help. Is it possible to auto detect the extension rather than manually entering like .jpg? Becuase i may have to upload zip rar extensions. Thanks

Viruthagiri
Light Poster
38 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Sure, check pathinfo() function, in my first code example the are all the values you get, extension included.

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: