Trying to upload a 7G file to S3 using the PHP SDK v2 (PHP 5.5 not available). File uploads less than 5G work great, but multipart uploads have never worked. They always end with no message or error at all, just before the upload should complete.
I have full S3 access. Have tried a bunch of different things to no avail.
Code is nothing special:
$uploader = UploadBuilder::newInstance()
->setBucket($bucket_nm)
->setKey($key)
->setMinPartSize(100 * 1024 * 1024)
->setConcurrency(1)
->setSource($src_path)
->setClient($s3)
->build();
try {
$uploader->getEventDispatcher()->addListener(
'multipart_upload.after_part_upload',
function($event) {
$msg = $event['state']->count() . ' parts uploaded.';
echo "$msg<br />";
WriteToLog($msg);
}
);
$uploader->upload();
$msg = 'Upload complete.';
} catch (MultipartUploadException $e) {
$uploader->abort();
$msg = 'Upload failed. ' . $e->getMessage() . '.';
}
echo "$msg<br />";
WriteToLog($msg);
You need to place apply these try catch in place of your for abort or upload process
// Perform the upload. Abort the upload if something goes wrong
try {
$uploader->upload();
echo "Upload complete.\n";
} catch (MultipartUploadException $e) {
$uploader->abort();
echo "Upload failed.\n";
}
Related
I'm trying to copy a file with a service account, and then grant access to my personal account. The copy seems to be working correctly which it seems to be copying the file to the google drive service account. So it's returning and ID that was created, but it fails when trying to insert the permissions on the file. says undefined method insert.
Here's what I have now
private function copy_base_file( $new_file_name )
{
$service = $this->get_google_service_drive( $this->get_google_client() );
$origin_file_id = "{id of file to copy}";
$copiedFile = new Google_Service_Drive_DriveFile();
$copiedFile->setName($new_file_name);
try {
$response = $service->files->copy($origin_file_id, $copiedFile);
$ownerPermission = new Google_Service_Drive_Permission();
$ownerPermission->setEmailAddress("{myemailhere}");
$ownerPermission->setType('user');
$ownerPermission->setRole('owner');
$service->permissions->insert("{sheet_id_here}", $ownerPermission,
['emailMessage' => 'You added a file to ' .
static::$applicationName . ': ' . "Does this work"]);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
The insert method is deprecated in the latest version (V3) of the Google Drive API.
Use the create method instead.
V3 Drive API Permissions Create
private function copy_base_file( $new_file_name )
{
$service = $this->get_google_service_drive( $this->get_google_client() );
$origin_file_id = "{id of file to copy}";
$copiedFile = new Google_Service_Drive_DriveFile();
$copiedFile->setName($new_file_name);
try {
$response = $service->files->copy($origin_file_id, $copiedFile);
$ownerPermission = new Google_Service_Drive_Permission();
$ownerPermission->setEmailAddress("{myemailhere}");
$ownerPermission->setType('user');
$ownerPermission->setRole('owner');
$service->permissions->create("{sheet_id_here}", $ownerPermission,
['emailMessage' => 'You added a file to ' .
static::$applicationName . ': ' . "Does this work"]);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
I am working with facebook graph api, in my code i want to store a user profile image url in my database & the image store in my database file, it can find the source file also show it but it can't store image my database. The error says that:
file_put_contents(celebrity_u_look_alike/youtube_star/fb_user_image/img_1264053943663652.png): failed to open stream: No such file or directory in /home/smartcarsassocia/public_html/celebrity_u_look_alike/youtube_star/youtube_star.php on line 101
My source code shown below :
try {
$requestPicture = $fb->get('/me/picture?redirect=false&height=250&width=250'); //getting user picture
$requestProfile = $fb->get('/me'); // getting basic info
$picture = $requestPicture->getGraphUser();
$profile = $requestProfile->getGraphUser();
$url= $picture['url'];
echo $url;
$filename = 'img_' . $profile_data['id'] . '.png';
echo $filename;
$path1 = "celebrity_u_look_alike/youtube_star/fb_user_image/" . basename($filename);
$image_file = file_get_contents($url);
file_put_contents($path1, $image_file );
//file_put_contents($path2, file_get_contents($url));
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
This is because:
file_put_contents($path1, $image_file );
the path you have provided in $path1 doesn't exist physically. So make sure the directory exist. If not, then create it using mkdir() function.
I develop a social android app that users upload high quality image to my server and then download them to show in list view in app feed.
Uploaded image save in ./uploads/ directory in my server. I use the below code in PHP to save image in server:
<?php
// Path to move uploaded files
error_reporting(E_ALL ^ E_DEPRECATED);
include 'conf.php';
$target_path = "uploads/";
// array for final json respone
$response = array();
// getting server ip address
$server_ip = gethostbyname(gethostname());
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip . '/' . 'AndroidFileUpload' . '/' . $target_path;
if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);
try {
// Throws exception incase file is not being moved
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// make error flag true
print "error1";
}
print basename($_FILES['image']['name']);
} catch (Exception $e) {
// Exception occurred. Make error flag true
print "error2";
}
} else {
// File parameter is missing
print "error3";
}
?>
Now I want to save low resolution of every image in different directory(or in real time get low resolution ) and get url of it and using php send to android app and users in my app can see a thumbnail or low resolution of image before download whole image.
How I should do it?
Easy. You can use the code below to do this :
<?php
$org_info = getimagesize("source image");
echo $org_info[3] . '<br><br>';
$rsr_org = imagecreatefromjpeg("source image");
$rsr_scl = imagescale($rsr_org, new_width, new_height, IMG_BICUBIC_FIXED);
imagejpeg($rsr_scl, "destination image");
imagedestroy($rsr_org);
imagedestroy($rsr_scl);
?>
Trying to get my script to send me an email if the file uploads successfully. Here is my part of the script that saves the file to the server as username.site.zip:
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
return array('success'=>true);}
else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
I'm not quite sure how to add in the mail function so that if it's success=>true then it sends mail('email#domain.com','subject','body');
Any help would be great.
<?php
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
$message = "The file was successfully uploaded.";
mail('email#domain.com', 'My Subject', $message);
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
?>
You had it. Not sure what you still needed.
Insert your mail command right before the return of the success=>true array like this:
if ($this->file->save($uploadDirectory . $_SESSION['myusername'] . '.site.' . $ext)){
mail('email#domain.com','subject','body');
return array('success'=>true);}
else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
Maybe encapsulate it in a try-catch block, but this should work right out of the box.
I have a php script that sends large files via FTP. After the file is sent I'm trying to write to the browser "success". I'm also trying to send a query to the database to record that the file was sent. However, any code that I have that comes after the ftp_put does not get executed.
if (ftp_put($conn_id, $upload_filename, $filename, FTP_BINARY))
{
echo "File Sent";
echo $upload_filename." - ".date("d/m/Y H:i:s")." - ".filesize($filename)." bytes<br>" ;
}
else
{
echo "Problem while Uploading $filename\n <br/>". $upload_filename ;
}
If ftp_put is false the echo works. But, if the ftp_put is a success any code I put there will not run.
The file size I am sending is 7,305kb
It is likely that the problem here is that your script is timing out while the file is uploading. Try adding this line before the code above:
set_time_limit(0);
The thing is that ftp_put() blocks any further action until the upload is finished. Try ftp_nb_put() (no blocking) like so:
$upload = ftp_nb_put($conn_id, $upload_filename, $filename, FTP_BINARY);
if($upload == FTP_MOREDATA)
{
echo 'Uploading ' . $upload_filename . ' - ' . date("d/m/Y H:i:s") . ' - ' . filesize($filename) . ' bytes<br />';
while($upload == FTP_MOREDATA)
{
echo '.'; //Output a . to page or do whatever
$upload = ftp_nb_continue($conn_id);
}
}
//Note: While in the while above, it will either end in FTP_FINISHED or FTP_FAILED
if($upload == FTP_FAILED)
{
echo "Problem while Uploading $filename\n <br />". $upload_filename;
}