Hello all,
Maybe you can help me.
I am trying to upload files to Amazon S3 but I am having an issue when I take a working example and make it a function in my controller.
So here is the working example in one file: aws.php
<?php
function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
//Here you can add valid file extensions.
$valid_formats = array("jpg", "png", "gif", "bmp","jpeg","PNG","JPG","JPEG","GIF","BMP");
$bucket = 'cesfiledata';
$s3 = new A2S3();
/*$objects = $s3->listObjects(array('Bucket' => $bucket));
foreach ($objects->get('Contents') as $object)
print('<p>' . $object['Key']) . '</p>';
//note this line is being overwritten but you get the idea
echo '<img src="https://s3-us-west-2.amazonaws.com/cesfiledata/'.$object['Key'].'">';
echo "This is the output: "."<br />";
*/
$msg='';
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$tmp = $_FILES['file']['tmp_name'];
$ext = getExtension($name);
if(strlen($name) > 0)
{
// File format validation
if(strlen($name) > 0)
{
// File size validation
if($size<(10024*10024))
{
//Rename image name.
$actual_image_name = time().".".$ext;
/*$s3->putBucket(array('Bucket' => $bucket,
'LocationConstraint'=> 'us-west-2',
'region'=>'us-west-2',
'ACL' => 'public-read'));*/
if($s3->putObject(array(
'SourceFile'=>$tmp,
'Bucket' => $bucket,
'Key'=>$_POST['folder'].'/'.$actual_image_name,
'ACL' => 'public-read',
'x-amz-storage-class' => 'REDUCED_REDUNDANCY'
)) )
{
$msg = "S3 Upload Successful.";
$s3file='http://'.$bucket.'.s3.amazonaws.com/'.$_POST['folder'].'/'.$actual_image_name;
echo "<img src='$s3file'/>";
echo 'S3 File URL:'.$s3file;
}
else
$msg = "S3 Upload Fail.";
}
else
$msg = "Image size Max 1 MB";
}
else
$msg = "Invalid file, please upload image file.";
}
else
$msg = "Please select image file.";
}
?>
<form action="" method='post' enctype="multipart/form-data">
Upload image file here
<input type='file' name='file'/> <input type='submit' value='Upload Image'/>
<select id="folder" name="folder">
<option value="images">Images</option>
<option value="documents">Documents</option></select>
<?php echo $msg; ?>
</form>
And here is the one in the controller:
public function actionAWSfiles()
{
$model = new Awsfiles;
$bucket = 'cesfiledata';
if(isset($_POST['Awsfiles']))
{
$model->attributes=$_POST['Awsfiles'];
$name = $_FILES['Awsfiles']['name'];
$tmp = $_FILES['Awsfiles']['tmp_name'];
$size = $_FILES['Awsfiles']['size'];
$actual_image_name = time().".".$this->getExtension($name['file']);
$model->href = $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name;
if(strlen($name['file'])>0) {
if($model->save()) {
$s3 = new A2S3();
//check file size.
if($size['file']<(10024*10024))
{
if($s3->putObject(array(
'SourceFile'=>$tmp,
'Bucket' => $bucket,
'Key'=>$actual_image_name,
'ACL' => 'public-read',
'x-amz-storage-class' => 'REDUCED_REDUNDANCY'
)))
{
Yii::app()->user->setFlash('notification','File has been successfully uploaded to Amazon!');
$this->redirect(array('/awsfiles/view/','id'=>$model->id));
}
else {
Yii::app()->user->setFlash('error','File upload failed!');
}
}
}
}
}
$this->render('_awscreate',array(
'model'=>$model),
false, true
);
}
Here is the form feeding the controller function:
<div class="wide form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'awsfiles-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data')
)); ?>
<!--<p class="note">Fields with <span class="required">*</span> are required.</p>-->
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php //echo $form->labelEx($model,'filename'); ?>
<?php echo $form->hiddenField($model,'filename',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'filename'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'href'); ?>
<?php echo $form->hiddenField($model,'href',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'href'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'uploaddate'); ?>
<?php echo $form->hiddenField($model,'upoaddate',array('value'=>date('Y-m-d'))); ?>
<?php //echo $form->error($model,'uploaddate'); ?>
</div>
<div class="row">
<?php //echo $form->labelEx($model,'uploaddate'); ?>
<?php echo $form->FileField($model,'file',array('onchange' => '
$("#Awsfiles_filename").val(removeFakePath($(this).val()));')); ?>
<?php //echo $form->error($model,'uploaddate'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<script type="text/javascript">
function removeFakePath(path) {
filename = path.replace(/^.*\\/, "");
return filename;
}
</script>
Any help will be much appreciated…
I believe the issue is with the Sourcefile getting the proper tmp path file name etc…