File upload not working correctly with PDF Converter - php

Hi I am using LiveDocX to convert .Doc Files to .PDF files upon user upload.
However the files returned from LiveDocX are blank.
I seem to think this has somthing to do with the template path (file being uploaded through the form)
Below is the code in question.
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$tmpPath = '/CMS/Custom/vault/tmp/'.md5($_REQUEST['ID']).'/';
$targetPath = '/CMS/Custom/vault/'.md5($_REQUEST['ID']).'/';
$targetFile = $targetPath . md5(time());
$filename = $_FILES['file']['name'];
$fname = substr($filename, 0,strrpos($filename,'.'));
$fi = new finfo(FILEINFO_MIME_TYPE);
$ext = $fi->file($_FILES['file']['tmp_name']);
$extention = end(explode('.', $filename));
// IF DOCUMENT IS PDF
if(strstr($ext, "pdf")){
$tmpFile = $_SERVER['DOCUMENT_ROOT'].'/CMS/Custom/vault/tmp'.md5($_REQUEST['ID']);
exec('convert '.$tempFile.' -resample 300 '.$tmpFile);
$tempFile = $tmpFile;
}
// IF DOCUMENT IS DOCX
if ($extention == 'docx') {
$path = $_SERVER['DOCUMENT_ROOT'].'/CMS/Custom/ZendFramwork/library/Zend/Service/docx';
require_once $path.'/common.php';
$docx = new Zend_Service_LiveDocx_MailMerge();
$docx->setUsername(DEMOS_ZEND_SERVICE_LIVEDOCX_USERNAME)
->setPassword(DEMOS_ZEND_SERVICE_LIVEDOCX_PASSWORD);
$docx->setLocalTemplate($tempFile);
$docx->assign('dummyFieldName', 'dummyFieldValue'); // necessary as of LiveDocx 1.2
$docx->createDocument();
$document = $docx->retrieveDocument('pdf');
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$tmpPath)){
mkdir($_SERVER['DOCUMENT_ROOT'].$tmpPath, 0755, true);
}
$pdf = $_SERVER['DOCUMENT_ROOT'].$tmpPath.$fname.'.pdf';
file_put_contents($pdf, $document);
unset($docx);
$tempFile = $pdf;
$filename = $fname.'.pdf';
}
// CREATE VAULT DIRECTORY.
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$targetPath)){
mkdir($_SERVER['DOCUMENT_ROOT'].$targetPath, 0755, true);
}
//CREATE MD5 FILE
$file = fopen($_SERVER['DOCUMENT_ROOT'].$targetFile, "w+");
if(fwrite($file, convert(file_get_contents($tempFile), md5($_REQUEST['ID'])))){
$targetFile = str_replace('/CMS/Custom/vault/', "", $targetFile);
$q = "INSERT INTO `member_vault` SET `member` = '{$_REQUEST['ID']}', `name` = '{$filename}', `ext` = '{$ext}', `path` = '{$targetFile}', `time` = '".time()."'";
mysql_query($q) or die(mysql_error());
unlink($tempFile);
echo $targetFile;
}
}

Related

How to upload Image or large file to temp folder before submit form in laravel

I'm using this code to upload files or images. It's working but can't upload a large file and I want to upload a file when selecting it from the local computer like the below image.
I use below PHP code in the controller.
$image = $request->file('file_upload');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
echo $new_name;
$image->move(public_path('images'), $new_name);
Try to use this code.
Change the $request->sharing_file with your field name.
You can increase the $size for image what you want, currently it is 16mb and working fine.
$myimage = $request->image;
$size = getClientSize();
if($sizes < 16777216){
$fileMimeType = explode('/', $myimage->getClientMimeType());
$fileType = $fileMimeType[0];
$originalFileName = substr($myimage->getClientOriginalName(), 0, strpos($myimage->getClientOriginalName(), "."));
$originalFileName = substr(str_replace(' ', '-', $originalFileName),0,10);
$rand = rand(9,1000);
$fileName = $rand.'-'.$originalFileName.'.'.$myimage->getClientOriginalExtension();
$upload = $values->move(public_path('images'), $fileName);
if($upload) {
$message = 'File Uploaded';
} else {
$message = "Failed to upload file";
}
}
else {
$message = 'Files size should be less than 16 MB.';
}
Try
if ($request->hasFile('file_upload')) {
$destinationPath = public_path().'/images/';
$file = $request->file_upload;
$fileName = time() . '.'.$file->clientExtension();
$file->move($destinationPath, $fileName);
$input['your_databse_table_field_name'] = $fileName;
}

imagescale function while uploading file

I try to implement a very simple scale function while uploading the file to the server. My host has 5.5.38 php version and gd is included and is 2.1.0 version.
The upload function worked until I tried to use the scale function. Obviously I am doing the wrong way but honestly I don't know what I am doing wrong and after a lot of research i am here to ask for your help..
This is the code that i use to upload file and scale it:
$id2 = $_POST['hiddenId'];
$ds = DIRECTORY_SEPARATOR;
// CREATE DIRECTORY
$dir = "gallery/g";
$dir .= $id2;
$dir .= "/";
$dir2 = '../../' . $dir;
if (!mkdir($dir2,0777,true));
$foldername = $dir;
if (!empty($_FILES)) {
/* DATA FOR DATABASE */
$images = '';
$images .= $dir;
$images .= $_FILES['file']['name'];
/* UPLOAD FILE */
$fileupload = basename( $_FILES['file']['name']);
$fileType = $_FILES['file']['type'];
$fileSize = $_FILES['file']['size'];
$tempFile = $_FILES['file']['tmp_name'];
//$targetPath = dirname( __FILE__ ) . $ds . $foldername . $ds;
// FILE DIRECTORY
$targetPath = '../../' . $foldername;
// TRY TO SCALE IMAGE
$imagenew = ''; // NEW VARIABLE
$imagenew = imagecreatefromjpeg($tempFile); // PASSING THE TEMPFILE
$imagescaled = imagescale($imagenew, 800); // SCALING THE FILE
$targetFile = $targetPath. $imagescaled;
// MOVE SCALED FILE TO THE SERVER
move_uploaded_file($tempFile,$targetFile);
// UPDATE DATABASE
update_gallery($id2,$images,$con);
}
i inspected the code with firebug but it returns no error so I don't know where to start to catch the error.. May you help me please?

uncaught exception: could not open file

so i'm trying to upload to uploadcare cdn and i get this error: Uncaught Exception: couldn't open file "ce3577f708f527ff570e0d21acb39c62.jpg"
$description = $_POST['uploaddesc'];
list($width,$height) = getimagesize($_FILES['uploadimage']['tmp_name']);
$extensions = array('image/jpeg','image/jpg','image/gif', 'image/png', 'image/tiff');
$move = 'uploads/';
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$extension = pathinfo($_FILES['uploadimage']['name']);
if(!$_FILES['uploadimage']['tmp_name']){
$errors[]="Please choose an image";
}
if(empty($description)){
$errors[]='Please choose a description for the image';
}
//IF IMAGE IS UPLOADED..
if($_FILES['uploadimage']['tmp_name']){
$mime = finfo_file($finfo, $_FILES['uploadimage']['tmp_name']);
// IF MIME FAILES OR GETIMAGESIZE IS NULL (meaning its no an image)
if(!getimagesize($_FILES['uploadimage']['tmp_name']) && !in_array($_FILES['uploadimage']['tmp_name'],$mime)){
$errors[]='Please choose an image';
unset($_FILES['uploadimage']['tmp_name']);
}
if(empty($errors)){
$randString = md5(time()); //encode the timestamp - returns a 32 chars long string
$fileName = $_FILES["uploadimage"]["name"]; //the original file name
$splitName = explode(".", $fileName); //split the file name by the dot
$fileExt = end($splitName); //get the file extension
$newFileName = strtolower($randString.'.'.$fileExt); //join file name and ext.
if(move_uploaded_file($_FILES['uploadimage']['tmp_name'], $move . $newFileName)){
$description = $_POST['uploaddesc'];
$user->insertArt($description, 'uploads/' . $newFileName, $user->getUsername($_SESSION['username']));
$file = $api->uploader->fromPath('uploads/' . $newFileName);
$file->store();
//throw new ErrorException('MEMORY USAGE ' . memory_get_peak_usage());
}
}
}
i have fixed it. i had to use realpath('uploads/' . $newfilename);

PHP fail to move file from temp folder to specified folder

<?php
error_reporting(E_ALL);
include "func.php";
connectToDatabase("localhost","root","","test");
$fileName = $_FILES['name'];
$fileSize = $_FILES['size'];
$fileType = $_FILES['type'];
$fileTempName = $_FILES['temp_name'];
$uploaderID = $_SESSION['id'];
$date = date("d/m-Y");
$uploadDir = "avatarStor/";
$filePath = $uploadDir . $fileName;
if(!$fileName){
echo "Error: value 'fileName' was undefined<br>";
}
$result = move_uploaded_file($fileTempName, $filePath);
if (!$result) {
echo "Error: File could not be uploadet: ".$fileName;
exit;
}
if(!get_magic_quotes_gpc()){
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$result = mysql_query("UPDATE users SET profilepicture='$fileName' WHERE id='$uploaderID'") or die(mysql_error());
?>
I am unable to move_uploaded_file(); in this uploading script. I went ahead and chmod 777 the folder, but nevertheless, it failed. PHP provides no errors when running.
Change $fileTempName = $_FILES['temp_name']; to $fileTempName = $_FILES['that_input_name']['tmp_name'];

Help With Renaming Files With Uploadify

This is my code -
<?php
session_start();
include('connect.php');
mysqli_select_db($connect, "users");
$s = "select * from name where sessionusername = '$u'";
$q = mysqli_query($connect, $s);
$f = mysqli_fetch_array($q);
$name = $f['name'];
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
// $fileTypes = str_replace('*.','',$_REQUEST['fileext']);
// $fileTypes = str_replace(';','|',$fileTypes);
// $typesArray = split('\|',$fileTypes);
// $fileParts = pathinfo($_FILES['Filedata']['name']);
// if (in_array($fileParts['extension'],$typesArray)) {
// Uncomment the following line if you want to make the directory if it doesn't exist
// mkdir(str_replace('//','/',$targetPath), 0755, true);
// Get the extension, and build the file name
//$extension = pathinfo($tempFile, PATHINFO_EXTENSION);
$extension = end(explode(".",$_FILES['Filedata']["name"]));
$new_file_name = '".$name."'".".$extension;
$targetFile = str_replace('//','/',$targetPath) . $new_file_name;
// $targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
?>
Why is the above not working? As you can see, I am trying to pull down the name from the users database, and then rename the uploaded file to the name that was pulled from the db.
Can you help me out? Thanks a lot.
Is form enctype == 'multipart/form-data' ?
Ah, now I understand what you're talking about. You should remove your other post. This is an error I've actually encountered with Uploadify before, but I'm not sure what is going on here, specifically. Definitely, checkout your enctype, but for debugging, I implemented this solution for error reporting with Uploadify here: http://www.uploadify.com/forums/discussion/14/upload-script-error-reporting/p1.

Categories