giridharanbtech
asked on
php file upload help.
Hey guys i have a remote upload script. I would like to use it with amazon s3 bucket.
Here is a good tutorial.
http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/
Its working perfectly. But the thing is i would like to integrate it with my remote upload script.
This is my remote upload script.
This is the nettuts tutorial code
Can somebody help me? Thanks
Here is a good tutorial.
http://net.tutsplus.com/tutorials/php/how-to-use-amazon-s3-php-to-dynamically-store-and-manage-files-with-ease/
Its working perfectly. But the thing is i would like to integrate it with my remote upload script.
This is my remote upload script.
<?php
//////////////////////////////////////////////
//Do Not Change Below Here////////////////////
//////////////////////////////////////////////
if (function_exists('curl_init'))
{
$snatch_system = 'curl';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Mediafire File copier</title>
</head>
<body>
<div id="main">
<h1 style="text-align: center;font-size: 35px;">Mediafire copier</h1>
<?php
$submit = $_POST['submit'];
if ($submit)
{
if (isset($password))
{
if ($_POST['password'] != $password)
{
die('<p><strong>Password incorrect!</strong></p>');
$error = true;
}
}
if (!$defaultDest)
{
$defaultDest = 'snatched';
}
if (!file_exists($defaultDest))
{
mkdir($defaultDest);
}
$sizelimit = $sizelimit * 1024;
$files = $_POST['file'];
$news = $_POST['new'];
$allfiles = $_POST['allfiles'];
$separateby = $_POST['separateby'];
if($allfiles != "")
{
$files = explode($separateby,$allfiles);
}
for($i=0;$i<count($files);$i++)
{
$file = trim($files[$i]);
$uploadfile = explode('/', $file);
$filename = array_pop($uploadfile);
$newfilename = $news[$i];
if (!$newfilename)
{
$newfilename = $filename;
}
if (!isset($file))
{
echo '<p><strong>Please enter a URL to retrieve file from!</strong></p>';
$error = true;
}
if (!isset($newfilename))
{
echo '<p><strong>Please enter a new file name!</strong></p>';
$error = true;
}
if ($error == false)
{
$dest = $defaultDest;
$ds = array($dest, '/', $newfilename);
$ds = implode('', $ds);
$newname_count = 0;
if (file_exists($ds))
{
echo '<p><strong>File already exists!</strong></p>';
$newname_count++;
$newfile = array($newname_count, $newfilename);
$newfile = implode('~', $newfile);
$newfile_ds = array($dest, '/', $newfile);
$newfile_ds = implode('', $newfile_ds);
while($renamed == false)
{
if (file_exists($newfile_ds))
{
$newname_count++;
$newfile = array($newname_count, $newfilename);
$newfile = implode('~', $newfile);
$newfile_ds = array($dest, '/', $newfile);
$newfile_ds = implode('', $newfile_ds);
}
else
{
$renamed = true;
}
}
$newfilename = $newfile;
$ds = $newfile_ds;
echo '<p>New file name is <strong>'.$newfile.'</strong>.</p>';
}
echo '<p><strong>Copying...</strong></p>';
if ($snatch_system == 'curl')
{
$ch = curl_init($file);
$fp = fopen($ds, 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
}
else
{
if (!copy($file, $ds))
{
echo '<p>Was unable to copy <a href="'.$file.'">'.$file.'</a><br />See if your path and destination are correct.</p>';
$copy_fail = true;
}
}
if ($copy_fail == false)
{
if ($sizelimit > 0 && filesize($ds) > $sizelimit)
{
echo '<p><strong>File is too large.</strong>';
unlink($ds);
}
else
{
echo '<p style="font-size:18px;"><strong>Copy successful!</strong></p>';
echo '<p><a href="'.$URLDest.'/'.$newfilename.'">Click here for file</a></p>';
if ($snatch_system == 'curl')
{
$size_dl = round($curl_info['size_download']/1024, 2);
$speed_dl = round($curl_info['speed_download']/1024, 2);
echo '<p>Downloaded '.$size_dl.'KB in '.$curl_info['total_time'].' seconds.<br />With an average download speed of '.$speed_dl.'KB/s.';
}
}
}
}
}
}
$self = $_SERVER['PHP_SELF'];
?>
<fieldset><legend>Mediafire file copier</legend>
<label for="file">Full path to file to copy</label>
<p>Example: http://foobar.com/image.png</p>
<?
$repeat = (isset($_REQUEST['repeat']))?($_REQUEST['repeat']):(1);
?>
<form method="POST" action="<?=$self?>">
<input type="text" name="repeat" size="10" value="<?=$repeat;?>">
<input name="Repeat" value="Repeat" type="submit">
</form>
<form method="POST" action="<?=$self?>">
<? for($i=0;$i<$repeat;$i++){?>
<br>File<?=($i+1)?> : <input type="text" name="file[]" size="45" value="">
<!--<input type="text" name="new[]" size="45" value="">-->
<? } ?>
<br>OR<br><br>
<textarea name="allfiles" cols="36" rows="10">http://www.somedomain.com/file1.zip##http://www.somedomain.com/file2.zip##http://www.somedomain.com/file3.zip</textarea>
Separate URL by:
<input type="text" value="##" name="separateby" size="5" value="<?=$separateby;?>">
<p>Example: image.png</p>
<? if (isset($password)){ ?>
<label for="password">Password</label>
<input type="password" name="password" id="password" size="45" value=""><br />
<? } ?>
<p><input name="submit" type="submit" id="submit" value="submit" accesskey="s"></p>
</form>
</fieldset>
</div>
</body>
</html>
This is the nettuts tutorial code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>S3 tutorial</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
//include the S3 class
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'CHANGE THIS');
if (!defined('awsSecretKey')) define('awsSecretKey', 'CHANGE THIS TOO');
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
//check whether a form was submitted
if(isset($_POST['Submit'])){
//retreive post variables
$fileName = $_FILES['theFile']['name'];
$fileTempName = $_FILES['theFile']['tmp_name'];
//create a new bucket
$s3->putBucket("yourbucket", S3::ACL_PUBLIC_READ);
//move the file
if ($s3->putObjectFile($fileTempName, "yourbucket", $fileName, S3::ACL_PUBLIC_READ)) {
echo "<strong>We successfully uploaded your file.</strong>";
}else{
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}
?>
<h1>Upload a file</h1>
<p>Please select a file by clicking the 'Browse' button and press 'Upload' to start uploading your file.</p>
<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>
<h1>All uploaded files</h1>
<?php
// Get the contents of our bucket
$contents = $s3->getBucket("yourbucket");
foreach ($contents as $file){
$fname = $file['name'];
$furl = "http://yourbucket.s3.amazonaws.com/".$fname;
//output a link to the file
echo "<a href=\"$furl\">$fname</a><br />";
}
?>
</body>
</html>
Can somebody help me? Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Please note "images_logo_lg.gif" is a file name i tried to upload. This is the full url of the file.
http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif
http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif
ASKER
Can somebody help me?
ASKER
Thankyou so much for the answer. But it still stores the files in my localdirectory. I mean in that "snatched" folder. I dont want my file to store in the local folder. Besides i'm getting this error.
If you need amazon s3 key for testing i can give it to you. I have all the codes. But i have no idea how to migrate the codes. I've tried everything. It would be awesome if you assist me. Thanks