I have a FPDF script that submits a PDF and moves it to a folder on the server.
I have a upload field in the form before the FPDF script is run named "file"
i am trying to move it to the same folder as the generated PDF.
below is my code: (the PDF is generated and moved to the folder but nothing happens with the uploaded file)
mkdir("claims/$name", 0777);
$move = "claims/$name";
foreach ($_FILES["files"]["tmp_name"] as $key => $value)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name2 = $move ."\\".basename($_FILES["files"]["name"][$key]);
move_uploaded_file($tmp_name, $name2);
}
$filename = "claims/$name/$name.pdf";
$pdf->Output($filename, 'F');
header('Location: home.php');
I was able to get this working with a few changes to my code. I also got it to work with multiple uploads.
// make the directory
mkdir("claims/$name", 0777);
$total = count($_FILES['files']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "claims/$name/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
Related
files are being uploaded and added to zip the only problem is to move the zip to desired location.the permission is OK i have moved single file. Now i want to create a zip containing multiple files and then moving that zip to folder.
$uploaddir = 'upload/page/';
if($_FILES['image_file_holder']['name'] != '')
{
$total = count($_FILES['image_file_holder']['name']);
echo $total;
$imagarr = explode(".", "myserver.zip");
$newimgfile = $imagarr[0]."_".mt_rand().'.'.$imagarr[1];
$zipname = $newimgfile;
$upload = $uploaddir . $newimgfile;
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['image_file_holder']['name'][$i];
if ($tmpFilePath != ""){
$zip->addFile($tmpFilePath);
}
}
$zip->close();
echo "moving";
if(move_uploaded_file($zipname,$upload))
{
echo "done";
}
}
I have some code running on my site which works well to upload single files from file input form elements - but I now need a multiple file input form element to accept more than 1 file and upload them all to the server and store the details of the filenames uploaded in a comma separated string... Any ideas on how to make this work with the code I am using below:
form field:
<input name="logoexamples[]" id="blogoexamples" type="file" class="textInput" value="notrelevant" multiple>
PHP code (that works to accept 1 file uploaded, but not more than 1....?):
<?php
// initialize output;
$output = true;
// valid extensions
$ext_array = array('pdf', 'txt', 'doc', 'docx', 'rtf', 'jpg', 'jpeg', 'png', 'eps', 'svg', 'gif', 'ai');
// create unique path for this form submission
//$uploadpath = 'assets/uploads/';
// you can create some logic to automatically
// generate some type of folder structure here.
// the path that you specify will automatically
// be created by the script if it doesn't already
// exist.
// UPLOAD TO FOLDER IN /ASSETS/UPLOADS/ WITH ID OF THE PARENT PROJECT FOLDER RESOURCE
// Get page ID
// $pageid = $modx->resource->get('id');
// $uploadpath = 'assets/uploads/'.$pageid.'/';
// Get parent page title
$parentObj = $modx->resource->getOne('Parent');
$parentpageid = $parentObj->get('pagetitle');
$uploadpath = 'assets/uploads/'.$parentpageid.'/';
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
// loop through files
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename( $_FILES[$sf]['name'] );
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
// is the file name empty (no file uploaded)
if($filename != '') {
// is this the right type of file?
if(in_array($ext, $ext_array)) {
// clean up file name and make unique
$filename = mb_strtolower($filename); // to lowercase
$filename = str_replace(' ', '_', $filename); // spaces to underscores
$filename = date("Y-m-d_G-i-s_") . $filename; // add date & time
// full path to new file
$myTarget = $target_path . $filename;
// JWD - save uploaded filenames as a session var to get it on the redirect hook
$_SESSION['briefing_submittedfiles_' . $sf] = 'http://www.example.com/assets/uploads/'.$parentpageid.'/'.$filename;
// create directory to move file into if it doesn't exist
mkdir($target_path, 0755, true);
// is the file moved to the proper folder successfully?
if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
// set a new placeholder with the new full path (if you need it in subsequent hooks)
$modx->setPlaceholder('fi.'.$sf.'_new', $myTarget);
// set the permissions on the file
if (!chmod($myTarget, 0644)) { /*some debug function*/ }
} else {
// File not uploaded
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
} else {
// File type not allowed
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
// if no file, don't error, but return blank
} else {
$hook->setValue($sf, '');
}
}
return $output;
I had something similar coded for my website, this code is super old so don't judge or use it directly. Just an example.
if(isset($_POST['upload'])){
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "../FOLDER NAME/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
copy($newFilePath, $newFilePath1);
$filename = basename($_FILES['upload']['name'][$i]);
// add $filename to list or database here
$result = "The files were uploaded succesfully.";
}else{
$result = "There was an error adding the files, please try again!";
}
}
}
I'm having a spot of bother with PHPMailer and have searched high and low for this information but nada.
Basically I have a form where the user can upload multiple images which are then saved to a folder on my server, then I need to send an email with the images attached inline at the bottom of the email via PHPMailer.
I can get it to put the first image as inline but the rest of the images do not appear..
Some code:
require '../PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
if(count($_FILES['upload']['name']) > 0){
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = "../reports/".$id."/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
$mail->AddEmbeddedImage($filePath, $shortname, $shortname);
$atts = '<img src="cid:'.$shortname.'">';
}
}
}
}
$mail->Body .= $atts;
Solution:
$dir2 = opendir('../reports/'.$id); // Open the directory containing the uploaded files.
$files = array();
while ($files[] = readdir($dir2));
rsort($files);
closedir($dir2);
foreach ($files as $file) {
if ($file != "." && $file != ".." && $file != 'resources' ){
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
$url = '../reports/'.$id.'/'.$file;
$mail->AddEmbeddedImage($url, $withoutExt);
$mail->Body .= '<img src="cid:'.$withoutExt.'">';
}
}
Iam working on an inhouse project where there is a multi part file uploading function is built. Iam running a windows 7 with wamp 2.5
The multiple file upload works perfect and the files are being uploaded into the specified directories. The upload directory has been shared on a Lan network so that the other users can also access the uploaded file. But the uploaded files cannot be viewed from another computer connected in the Lan. There are lot of folder creations and the Lan systems are able to browse through the folders. But the files are not able to be viewed. Iam pasting the code below
//File upload section
if(count($_FILES['upload']['name']) > 0){
if (!file_exists('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername))
{
mkdir('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername, 0777);
}
if (!file_exists('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername.'/01_'.$product))
{
mkdir('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername.'/01_'.$product, 0777);
}
$pathtoupload= "Other_Orders/".$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername."/01_".$product;
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = $pathtoupload."/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
I have found the answer myself
The files uploaded through the wamp will never get the full permission from the windows. The only way out is to add a code to copy the uploaded file by renaming it with an underscore prefix or something like that so that the new file will get the full permission and will be readable and editable by the other windows users connected to the Lan. The code will be somewhat like below (in connection with the code mentioned in the question)
//Loop through each file
for($i=0; $i
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
$share_name = "_".$shortname;
//save the url and the file
$filePath = $pathtoupload."/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
$source_dir = $pathtoupload."/".$shortname;
$target_dir = $pathtoupload."/".$share_name;
copy($source_dir,$target_dir);
unlink($source_dir);
I got this working but when i look into the zip folder all the files size is 0. I tryed not adding the files to the zip and that worked, but when i try to add them to a zip the size goes to 0. Why is this.
here is my php
if(isset($_FILES['file'])){
$file_folder = "uploads/";
$zip = new ZipArchive();
$zip_name = time().".zip";
$open = $zip->open("zip/".$zip_name, ZipArchive::CREATE);
if($open === true){
for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
$filename = $_FILES['file']['name'][$i];
$tmpname = $_FILES['file']['tmp_name'][$i];
move_uploaded_file($tmpname, "uploads/".$filename);
$zip->addFile($file_folder, $filename);
}
$zip->close();
if(file_exists("zip/".$zip_name)){
// zip is in there, delete the temp files
echo "Works";
for($i = 0; $i < count($_FILES['file']['name']); $i++)
{
$filenameu = $_FILES['file']['name'][$i];
unlink("uploads/".$filenameu);
}
} else {
// zip not created, give error
echo "something went wrong, try again";
}
}
}
Your problem lies with this line: $zip->addFile($file_folder, $filename);
Currently that passes a path to the /uploads/ directory as the first argument.
According to Zip::addFile documentation you should be passing the path to the file to add (this includes the file and extension).
So change your code to include the file name (you already have it as a variable $filename which is handy).
$zip->addFile($file_folder.$filename, $filename);