Salut, je suis nouveau à Amazon et j'ai très peu d'idée de travailler avec des fichiers.J'essaie de télécharger une image miniature dans le seau. Je pourrais télécharger une image dans localhost mais je ne pouvais pas le télécharger en bucket.I pourrait télécharger l'image originale dans le seau, voici mon codeComment télécharger un fichier vignette dans un seau amazon s3?
<?php
//include the S3 class
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', '');
if (!defined('awsSecretKey')) define('awsSecretKey', '');
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
function Upload($field_name = '', $target_folder = '', $file_name = '', $thumb = FALSE, $thumb_folder = '', $thumb_width = '', $thumb_height = ''){
//folder path setup
$target_path = $target_folder;
$thumb_path = $thumb_folder;
//file name setup
$filename_err = explode(".",$_FILES[$field_name]['name']);
$filename_err_count = count($filename_err);
$file_ext = $filename_err[$filename_err_count-1];
if($file_name != '')
{
$fileName = $file_name.'.'.$file_ext;
}
else
{
$fileName = $_FILES[$field_name]['name'];
}
//upload image path
$upload_image = $target_path.basename($fileName);
//upload image
if(move_uploaded_file($_FILES[$field_name]['tmp_name'],$upload_image))
{
//thumbnail creation
if($thumb == TRUE)
{
$thumbnail = $thumb_path.$fileName;
list($width,$height) = getimagesize($upload_image);
$thumb_create = imagecreatetruecolor($thumb_width,$thumb_height);
switch($file_ext){
case 'jpg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'jpeg':
$source = imagecreatefromjpeg($upload_image);
break;
case 'png':
$source = imagecreatefrompng($upload_image);
break;
case 'gif':
$source = imagecreatefromgif($upload_image);
break;
default:
$source = imagecreatefromjpeg($upload_image);
}
imagecopyresized($thumb_create,$source,0,0,0,0,$thumb_width,$thumb_height,$width,$height);
switch($file_ext){
case 'jpg' || 'jpeg':
imagejpeg($thumb_create,$thumbnail,72);
break;
case 'png':
imagepng($thumb_create,$thumbnail,50);
break;
case 'gif':
imagegif($thumb_create,$thumbnail,50);
break;
default:
imagejpeg($thumb_create,$thumbnail,50);
}
}
return $fileName;
}
}
if(!empty($_FILES['image']['name'])){
$image_name = $_FILES['image']['name'];
$tmp_name = $_FILES['image']['tmp_name'];
$size = $_FILES['image']['size'];
$type = $_FILES['image']['type'];
$error = $_FILES['image']['error'];
$target_dir = "products/";
$target_file = $target_dir.$_FILES['image']['name'];
$fileTempName = $_FILES['image']['tmp_name'];
$fileName = $target_dir.$_FILES['image']['name'];
$s3->putBucket("bucketname", S3::ACL_PUBLIC_READ);
//move the original file to bucket file
if ($s3->putObjectFile($fileTempName, "bucketname", $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>";
}
//call thumbnail creation function and store thumbnail name
$upload_img = Upload('image','uploads/','',TRUE,'uploads/thumbs/','200','160');
$upload_img1 = Upload('image','products/','',TRUE,'products/','200','160');
$fileTempName12 = $_FILES[$upload_img1]['tmp_name'];
//$fileName12 = 'products/'.$_FILES[$upload_img1]['name'];
//full path of the thumbnail image
$thumb_src = 'products/'.$upload_img1;
$s3->putBucket("bucketname", S3::ACL_PUBLIC_READ);
//move the thumbnail to bucket
if ($s3->putObjectFile($fileTempName12, "bucketname", $thumb_src, S3::ACL_PUBLIC_READ)) {
echo "<strong>We successfully uploaded your file.</strong>";
}else{
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
//set success and error messages
$message = $upload_img?"<span style='color:#008000;'>Image thumbnail have been created successfully.</span>":"<span style='color:#F00000;'>Some error occurred, please try again.</span>";
}else{
//if form is not submitted, below variable should be blank
$thumb_src = '';
$message = '';
}
?>
<?php
// Get the contents of our bucket
$contents = $s3->getBucket("bucketname");
if(is_array($contents) || is_object($contents))
{
foreach ($contents as $file){
$fname = $file['name'];
$furl = "http://bucketname.s3.amazonaws.com/".$fname;
//output a link to the file
echo "<a href=\"$furl\">$fname</a><br />";
}
}
?>
<!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" />
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="image"/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
Mon fichier d'origine est téléchargé avec succès mais je reçois l'erreur suivante lors du chargement de l'image miniature,
Warning: S3::putBucket(): [BucketAlreadyOwnedByYou] Your previous request to create the named bucket succeeded and you already own it. in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 188
We successfully uploaded your file.upload google.jpgupload1
Notice: Undefined index: in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/index.php on line 130
Notice: Undefined variable: putObjectFile in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 187
Warning: S3::putBucket(): [BucketAlreadyOwnedByYou] Your previous request to create the named bucket succeeded and you already own it. in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 188
Warning: S3::inputFile(): Unable to open input file: in /Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/S3.php on line 224
Something went wrong while uploading your file... sorry.
Quelqu'un peut-il m'aider? Merci d'avance ...
Je reçois cette erreur en utilisant $ this lorsqu'il n'est pas dans le contexte de l'objet dans/Applications/XAMPP/xamppfiles/htdocs/image_thumbnail_creation_php/index.php sur la ligne 74 @kelvin kantaria – Anu
Je résous l'erreur. J'utilise api dans le fichier de classe que j'utilise $ this. vous enlevez $ this et incluez aussi le fichier de classe S3 –
Son fonctionnement ... incroyable Merci @kelvin kantaria – Anu