Converting Pdf file to images with Imagick and PHP - php

Program to load pdf image and at the same time convert them to jpg using Imagick.But couldnt convert and load it in Destination directory.
$name = $_FILES['file']['name'];
$fileName = substr($_FILES['file']['tmp_name'], 5).".".$ext;
date_default_timezone_set('UTC');
$fileDate = date('d.m.Y');
$fileSize = $_FILES['file']['size'];
$folder = $_POST['folder'];
$uploadfile1="$media_dir/$fileName";
$imagick = new imagick();
$imagick->readImage($uploadfile1);//line 149
$imagick->setImageFormat('jpg');
foreach($imagick as $i=>$imagick)
{
$imagick->writeImage($uploadfile1. " page ". ($i+1) ." of ". $pages.".jpg");
}
Error
Fatal error: Uncaught ImagickException: unable to open image
`/opt/ama/mediaFiles/phpe765pr.pdf': No such file or directory #
error/blob.c/OpenBlob/2701 in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php:149Stack
trace:#0
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php(149):
Imagick->readimage('/opt/gpssi/medi...')#1 {main} thrown in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/gpssi/modules/mediaFiles/uploadFile.php on line 149

You have a problem with the path of
/opt/ama/mediaFiles/phpe765pr.pdf
Make sure the path exists and the necessary privileges are given to all the folders in the path along the file to read it.

Related

PHP rename function error

I am trying to rename a file after getting the file name using basename(). I am getting an error saying 'cannot find the file specified'.
My code is :
$target_dir = "uploads/";
$base_name = basename($_FILES["file_to_upload"]["name"]);
$target_file = $target_dir.$base_name;//specifies the path of the file to be
uploaded.
$image_extention = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//rename the file
$rename_file_name = rename($base_name,'Image_1');
dd($rename_file_name);
When I run the code i get the following error:
Warning: rename(RageFace.jpg,Image_1): The system cannot find the file specified. (code: 2) in C:\xampp\htdocs\pdo\file_upload_handle.php on line 23
bool(false)
try
changing name while moving
move_uploaded_file($_FILES["file_to_upload"]["tmp_name"],$target_file);

Get file size codeigniter

This gives a Warning Message: filesize(): stat failed for http://localhost/wft/uploads/4_Sat_Sep_10_2016_16_18_52.pdf
$file_path = base_url().'uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
how to find file size of pdf.
You could do this without using the URI, instead using the path to the File.
<?php
// CHANGE __DIR__ TO FIT WITH THE PATH TO THE ROOT DIRECTORY HOLDING THE PDF FILE
$file_path = __DIR__ . '/uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
PHP filesize() function give the file size.
$path = "uploads/your_file";
$size = filesize($path);
NB: Path should be a relative path.

Image Upload in CakePHP on localhost

i am getting an error in uploading image into folder. there is an error occured during uploading that image.
my Controller Code is (cakeplus is my root folder ex: htpp://localhost/cakeplus) :
$directory = "http://".$_SERVER['HTTP_HOST'].'/cakeplus/pics';
if(!is_dir($directory)) {
mkdir($directory,0777);
}
$files_array = $this->data['Photo']['path'];
//pr($files_array); die;
if(isset($files_array['name']) && $files_array['name']!='') {
$filetype = $files_array['type'];
$filesize = $files_array['size'];
$filename = $files_array['name'];
$filetmpname = $files_array['tmp_name'];
$file_type = explode('.',$filename);
$ext_name = array_reverse($file_type);
$final_file_title = $ext_name[1];
$file_name = Inflector::slug( str_replace( ".".$ext_name[0], "" , $filename ). '_' .time() ,'-' );
$newFileName = $file_name.'.'.$ext_name[0];
move_uploaded_file($filetmpname, $directory.'/'.$newFileName);
$requirementuploadData['Photo']['path'] = $file_name;
$this->Photo->create();
$this->Photo->save($requirementuploadData,false);
}
Error(s)(Warnings) :
Warning (2): move_uploaded_file(http://localhost/cakeplus/pics/wallpaper-1433586197.png): failed to open stream: HTTP wrapper does not support writeable connections [APP\Controller\PhotosController.php, line 31]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA80D.tmp' to 'http://localhost/cakeplus/pics/wallpaper-1433586197.png' [APP\Controller\Photos
Look into the CakePHP Upload plugin - it will abstract away much of the work that goes into dealing with file and image uploads.
The error you are seeing is because you cannot use move_uploaded_file() to transfer from a file path (C:\xampp\tmp\phpA80D.tmp) to an HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png).
If you don't want to use the Upload plugin, and would prefer to keep working with what you already have, I would start by changing the $directory path. Something like this might be more appropriate:
$directory = WWW_ROOT . 'pics';
This will contain the path to your ./yourapp/webroot/pics directory, which is also the location of http://yourapp.com/pics.
Check out the documentation for more predefined paths.
may be folder dont have permission to write an image.
you should to use cakephp upload component.
$this->Upload->upload($file,$destination,$name);

How to specify path for the folder in Windows

I have a form that lets the user to upload files to the server, but I did not know how to write the correct path for that folder in the server which I want the files to be store in, also how to get the path for specific file to download later.
The path in the server:
/public_html/upload_files
The error I am getting:
Warning: move_uploaded_file(upload_files/project_guidelines.pdf):
failed to open stream: No such file or directory in
D:\sites\dwts.com\public_html\website\creat.php on line 50 Warning:
move_uploaded_file(): Unable to move 'C:\Windows\Temp\php14AC.tmp' to
'upload_files/project_guidelines.pdf' in
D:\sites\dwts.com\public_html\website\creat.php on line 50 Error
uploading file
Code:
$len = count($_FILES['Attachment']['name']);
for($i = 0; $i < $len; $i++) {
$uploadDir = 'upload_files/';
$fileName = $_FILES['Attachment']['name'][$i];
$tmpName = $_FILES['Attachment']['tmp_name'][$i];
$fileSize = $_FILES['Attachment']['size'][$i];
$fileType = $_FILES['Attachment']['type'][$i];
$filePath = $uploadDir . basename($_FILES['Attachment']['name'][$i]);
$result = move_uploaded_file($tmpName,$filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$uploadDir = 'upload_files/';
Where is the drive? Its Windows, so it needs a drive:
$uploadDir = 'c:/upload_files/';
You can't give a partial location to the second argument of move_uploaded_file...it needs a full path.

Amazon S3 Upload PDF and Create Cover Image

I have the following bit of code which uploads a PDF to Amazon S3, what I need to do is create an image from the 1st page of the PDF and upload that to s3 as well.
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
//check whether a form was submitted
if($_SERVER['REQUEST_METHOD'] == "POST")
{
//retreive post variables
$fileName = $_FILES['file']['name'];
$fileTempName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$extension=end(explode(".", $fileName));
$rand = rand(1,100000000);
$sha1 = sha1($rand);
$md5 = md5($sha1);
$fName = substr($md5, 0, 20);
$finalName = $fName.'.'.$extension;
//create a new bucket
$s3->putBucket("bucket", S3::ACL_PUBLIC_READ);
//move the file
if ($s3->putObjectFile($fileTempName, "bucket", 'publications/'.$finalName, S3::ACL_PUBLIC_READ)) {
$s3file='http://bucket.s3.amazonaws.com/publications/'.$finalName;
$aS3File = 'publications/'.$finalName;
$im = new imagick($s3file[0]);
// convert to jpg
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(75);
$im->setImageFormat('jpeg');
//resize
$im->resizeImage(640, 877, imagick::FILTER_LANCZOS, 1);
//write image on server (line 54)
$s3->putObjectFile("","bucket", 'publications/'.$im->writeImage($fName.'.jpg'), S£::ACL_PUBLIC_READ);
}else{
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
I have replace my actual bucket name with 'bucket' for security, can anyone tell me what I am doing wrong here as I get the following error:
PHP Fatal error: Uncaught exception 'ImagickException' with message
'Unable to read the file: h' in
/var/www/ams/pub-new-issue.php:45\nStack trace:\n#0
/var/www/ams/pub-new-issue.php(45): Imagick->__construct('h')\n#1
{main}\n thrown in /var/www/ams/pub-new-issue.php on line 45,
thanks
$s3file='http://bucket.s3.amazonaws.com/publications/'.$finalName;
$im = new imagick($s3file[0]);
$s3file is a string, but you're accessing an array index in it. As a result, you fetch the first character, h. Use just $s3file in your Imagick instantiation and you should be fine.

Categories