I have this script
<?php
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = dirname(__FILE__) . $_POST['folder'] . '/';
$pathinfoFile = pathinfo($_FILES['Filedata']['name']);
$targetFile = str_replace('//', '/', $targetPath) . uniqid() . '.' . $pathinfoFile['extension'];
move_uploaded_file($tempFile, $targetFile);
}
This script is from Uploadify, modified for saving the file with unique name. I need to save the temporary, unique and original names after the user has uploaded the file. These values will be used when the user finally submits the form. I have tried to save these values in $_SESSION but I'm having the problem outlined here:
http://uploadify.com/forum/viewtopic.php?f=5&t=43 ,
I tried the solution from forum but it did not work, is there an easier way to solve this problem?
Sorry i tried one more time with $_POST['PHPSESSID'] and is working right now , thanks for help
Related
I am using Laravel and I have a form that uploading a file. And I want to save the full path of that file in my database. Do you know how?
if($request->hasFile('alternate_add_file_path')) {
$file = $request->file('alternate_add_file_path');
$destination = 'files/';
$extension = $file->getClientOriginalExtension();
$file_name = $getCreateDate . '_' . str_replace('/','_',$M_USER_NAME) . '.' . $extension;
$file->move($destination, $file_name );
}
This below is for the save into database
$M_FILE_PATH = $file_name;
I do not have an error though, just only the filename which saved into my database, not the full path.
To convert a relative path to an absolute one (server side) you can use realpath. Eg.
$fullpath = realpath($file_name);
I'm attempting to move an uploaded image (from Android) that is to be renamed via the PHP below in the second example so that their names cannot conflict. The original example below uploads files correctly but can have naming conflicts. The error that I'm experiencing is that the move_uploaded_files function fails, which I'm unsure as to why. The directory appears the same but I could be wrong and the problem is that image is never moved from the temp directory. Above all else, I think this is just a directory issue since the original example works. Please let me know if you need more information. The example I'm going by is located here: How to rename uploaded file before saving it into a directory?
Original:
$uploaddir = './appphotos/';
$absPath = 'https://'.$_SERVER['HTTP_HOST'].'/complaint_desk/appphotos/';
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$newName = $uploaddir . $uploadFile;
New Attempt:
$temp = explode(".",$_FILES["userfile"]["name"]);
echo json_encode($temp);
$newfilename = rand(1,99999) . '.' .end($temp);
echo json_encode($newfilename);
$uploadFile = move_uploaded_file($_FILES["userfile"]["name"], "/var/www/complaint_desk/appphotos/" . $newfilename); echo json_encode($uploadFile);
You should use the function as follow:
if(move_uploaded_file($_FILES["userfile"]["tmp_name"], "./appphotos/" . $newfilename)) {
echo json_encode($uploadFile); // why do you want to encode it?
} else {
echo 'File failed to move';
}
Always check the result of move_uploaded_file(). Also, the file is located at $_FILES["userfile"]["tmp_name"] before moving.
Also, $absPath is incorrect. It shouldn't start with http protocol. It should look like /var/www/complaint_desk/appphotos/ or C:/complaint_desk/appphotos/.
I'm using uploadify on a project, and I've got the php script renaming a single file, but I'm unsure how to repeat the process if more than one file is uploaded at a time?
My php script is below...
$targetFolder = '/img/uploads'; // Relative to the root
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$fileParts = pathinfo($_FILES['Filedata']['name']);
$unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT);
$targetFile = rtrim($targetPath,'/') . '/' . $unique_hash .'-'.$_POST['userId'].'.'. $fileParts['extension'];
#$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
When you use ajax, you can send a request for every file just one by one then you wont have to use a foreach or you send the form at once and use the code below here.
When you send all file together you will be using in your form something like this: name=userfile[] so you can foreach the files like this:
foreach ($_FILES as $file)
{
$uploadfile = $uploaddir . basename($file['name']);
if (!move_uploaded_file($file["tmp_name"], $uploadfile))
{
echo set_e('error','Image ['.$i.'] not uploaded','');
}
}
Have you tried to do more than one? Uploadify uses jQuery ajax to process the files? It might look simultaneous but it processes one file at a time via ajax. It will call your upload.php as many times as there are files in the queue. So if your function works for one file, it will for the rest. You just need to make sure it's writing a unique filename in the directory every time the function is called whic it looks like it does. Uploadify handles looping through each file name then calls your upload script.
http://www.uploadify.com/demos/
Look at the network console with Chrome or Firebug and upload a bunch of pictures with on the demo. you'll see the upload.php script is called for each file.
FYI,
I used to use uploadify but I changed to PLUPLOAD http://www.plupload.com/. Supports multiple uploads too but better documented and easier to use with great api's.
I am using Uploadify for my script.
The main page:
<?php
session_start();
var_dump($_SESSION);
$uploaded_files = $_SESSION['uploaded_files'];
?>
//Uploadify, HTML forms and more (not related, No PHP in this section)
uploadify.php:
<?php
session_start();
require_once('includes/functions.php');
// Define a destination
$targetFolder = 'uploads/temp'; // Relative to the root
if (!empty($_FILES)) {
$fileParts = pathinfo($_FILES['Filedata']['name']);
$file_hash = GenRndStr(20) . '.' . $fileParts['extension'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $file_hash;
// Validate the file type
$fileTypes = array(); // File extensions
move_uploaded_file($tempFile, $targetFile);
$_SESSION['uploaded_files'][] = $file_hash;
echo '1';
}
?>
I'm sure it gets to the $_SESSION['uploaded_files'][] = $file_hash; part, since the actual file is uploaded to the directory. My problem is that the var_dump of $_SESSION['uploaded_files'] returns null.
The files are in the same directory level.
Thanks in advance.
I found the solution. The problem is that Uploadify's flash version is treated as a different client for the server, therefore the server creates a new session id for it.
I followed this topic: http://www.uploadify.com/forum/#/discussion/43
Hope it'll help others.
Who can help me to fix the following problem? Here is the issue: in a form POST i made people can upload files. The code below check if in the "uploads" folder there another file with the same name. If so, files are renamed as this example:
hallo.txt
1_hallo.txt
2_hallo.txt
... and so on.
This is the code used:
$OriginalFilename = $FinalFilename = $_FILES['uploaded']['name'];
// rename file if it already exists by prefixing an incrementing number
$FileCounter = 1;
while (file_exists( 'uploads/'.$FinalFilename ))
$FinalFilename = $FileCounter++.'_'.$OriginalFilename;
I would like to rename files in a different way. progressive numbers should be AFTER the file and, of course, before the extention. This is the same example of before but in the way i want:
hallo.txt
hallo_1.txt
hallo_2.txt
... and so on.
How can i modify the code to reach that result?
Thank you in advance and sorry for my newbie-style question. I'm really newbie! :)
Mat
Just change the $FinalFilename:
$FinalFilename = pathinfo($OriginalFilename, PATHINFO_FILENAME) . '_' . $FileCounter++ . '.' . pathinfo($OriginalFilename, PATHINFO_EXTENSION);
Or (better if you have a lot of files with the same name and often iterate more than once):
$filename = pathinfo($OriginalFilename, PATHINFO_FILENAME);
$extension = pathinfo($OriginalFilename, PATHINFO_EXTENSION);
while (file_exists( 'uploads/'.$FinalFilename ))
$FinalFilename = $filename . '_' . $FileCounter++ . '.' . $extension;