PHP fail to move file from temp folder to specified folder - php

<?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'];

Related

Saving Multiple File path to mysql using PHP

Good Day. I have a php script that move multiple file in my directory..
$filepath = 'uploads/';
if (isset($_FILES['file'])) {
$file_id = $_POST['file_id'];
$count = 0;
foreach($_FILES['file']['tmp_name'] as $k => $tmp_name){
$name = $_FILES['file']['name'][$k];
$size = $_FILES['file']['size'][$k];
if (strlen($name)) {
$extension = substr($name, strrpos($name, '.')+1);
if (in_array(strtolower($extension), $file_formats)) { // check it if it's a valid format or not
if ($size < (2048 * 1024)) { // check it if it's bigger than 2 mb or no
$filename = uniqid()."-00000-". $name;=
$tmp = $_FILES['file']['tmp_name'][$k];
if (move_uploaded_file($tmp_name, $filepath . $filename)) {
$id = $file_id;
$file_path_array = array();
$files_path = $filepath . $filename;
$file_extension = $extension;
foreach($file_name as $k_file_path => $v_file_path){
$file_path_array[] = $v_file_path;
}
foreach($file_extension as $k_file_extension){
$file_extension_array[] = $v_file_extension;
}
$file_path = json_encode($files_path);
$file_name = str_replace("\/", "/",$file_path);
var_dump($file_name);
$update = $mysqli->query("UPDATE detail SET file_path='$file_name' WHERE id='$id'");
} else {
echo "Could not move the file.";
}
} else {
echo "Your file is more than 2MB.";
}
} else {
echo "Invalid file format PLEASE CHECK YOU FILE EXTENSION.";
}
} else {
echo "Please select FILE";
}
}
exit();
}
this is my php script that move file to 'uploads/' directory and i want to save the path to my database. i try to dump the $file_name and this is my example path how to save that to my database.. ? any suggestions ?
NOTE: i already move the file to uploads/ directory and i only want to save the path to my database
string(46) "uploads/5638067602b48-00000-samplePDF.pdf"
string(46) "uploads/5638067602dee-00000-samplePDF1.pdf"
string(46) "uploads/5638067602f8d-00000-samplePDF2.pdf"
if you must store them in one field..
inside the loop
$file_name_for_db[]=$file_name;
outside the loop:
$update = $mysqli->query("UPDATE detail SET file_path='".json_encode($file_name_for_db)."' WHERE id='$id'");
there is serialize() instead of json_encode() if you prefer

Change name of uploaded file

I have been trying to change the name of a file after an upload with my script.
I want every file to be named as "testing". Later I am going to change "testing" to a
variable that gets a unique name. Now I just want to change the name to "testing".
Also the script always says error although the file are uploaded.
Can I get some help here please?
Here is my code:
<?php
$uploadDir = 'images/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO $db_table ( Image ) VALUES ('$filePath')";
mysql_query($query) or die('Error, query failed');
}
?>
I think you need
$fileName = "testing"; //maybe add extension
instead of getting original filename from $_FILES. Although after the file is moved you may end up with a situation of overwriting existing files as they all has the same name. To prevent that (for testing purposes) you may add something to $fileName, maybe a short random string.

Uploading files sequentially

I want to upload files sequentially. When I do so, the files I uploaded the last will get to the file system whereas my php code will not see the files I uploaded earlier. Anyone had a similar problem?
function inputImages()
{
print_r($_FILES);
$images_number = count($_FILES['images']['name']);
$uploadDir = 'tmp_name/';
$images = array();
for ($i=0; $i<$images_number; $i++) {
$fileName = $_FILES['images']['name'][$i];
$tmpName = $_FILES['images']['tmp_name'][$i];
$permanentname = sha1($tmpName.$fileName.rand()).substr($fileName,-4);
$filePath = $uploadDir . $permanentname;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
$ErrorMessage.= "Error uploading <strong>file</strong>";
} else {
array_push($images, $permanentname);
}
}
return $images;
}

File upload not working correctly with PDF Converter

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;
}
}

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