I am using Uploadify to upload a file. The filename is randomized by the PHP when uploaded. I need to echo the file name back to the JS. I don't know enough PHP to do this.
Here is my PHP Script:
<?php
$targetFolder = '/uploads';
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = sprintf('%s/%s.%s', $targetPath, uniqid(), $fileParts['extension']);
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','doc','docx','pdf','xlsx','pptx','tiff','tif','odt','flv','mpg','mp4','avi','mp3','wav','html','htm','psd','bmp','ai','pns','eps'); // 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.';
}
}
?>
If I understand the script correctly where the echo '1' at the bottom of the script is where the file name should be. How do I insert the changed or randomized file name there?
Your $targetFile variable contains the full path to your uploaded file (once moved). If you just want the filename part, use basename(), eg
echo basename($targetFile);
See http://php.net/basename
Related
I know that move_uploaded_file() sets the name of the uploaded file and sets the destination also. I have this:
$sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/'.$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ; // Moving Uploaded file`
I have tried explode $_FILES['file']['tmp_name'] but I don't get to change the name of my uploaded file to my POST variable $newfile=$_POST["something"];
Thank you in advance
I am using
//File name
$file_name = $_FILES["file"]["name"];
$file_name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file_name);
// get extension
$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
// change name
$imagename = $file_name . time() . "." . $ext;
Set the entire upload path with the file name in $targetpath variable.
In you code
$sourcePath = $_FILES['file']['tmp_name'];
$newfile=$_POST["something"]; //any name sample.jpg
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/img/profiles/'.$newfile;
move_uploaded_file($sourcePath,$targetPath) ;
Now the uploaded file name is sample.jpg
I think this will helpful for you.
I am using Uploadify and when a person uploads the file I want the file name to be a random generated file name. Numbers would be fine.
Here is the current PHP I am using:
<?php
$targetFolder = '/uploads';
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','doc','docx','pdf','xlsx','pptx','tiff','tif','odt','flv','mpg','mp4','avi','mp3','wav','html','htm','psd','bmp','ai','pns','eps'); // 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.';
}
}
?>
I tried replacing this:
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
with this (per this answer):
$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = rtrim($targetPath,'/') . '/' .rand_string(20).'.'.$fileParts['extension'];
but that did not work, in fact, it stopped the file from uploading at all.
How do I modify the script to create random file names? Note: I know almost no PHP, please make any answer clear for a beginner.
I'd use uniqid() as I have no idea what rand_string is or where it comes from, eg
$fileParts = pathinfo($_FILES['Filedata']['name']);
$targetFile = sprintf('%s/%s.%s', $targetPath, uniqid(), $fileParts['extension']);
Another thing I never do is rely on DOCUMENT_ROOT. Instead, use a path relative from the current script. For example, say your script is in the document root
$targetPath = __DIR__ . '/uploads';
I am trying to use Uploadify on my site and have it setup with the following uploadify.php:
<?php
// Define a destination
//$targetFolder = '/uploads'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'];
$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.';
}
}
?>
It wasn't uploading so I commented out $targetFolder and changed $targetPath so that it is just the document root. This results in $targetFile being /home/user/public_html/example.com/file.txt when I try to upload file.txt with Uploadify. The folder is set to 755. I'm at a loss as to what the problem could be. I am otherwise using a vanilla install of Uploadify and everything seems to work fine except that the file never actually goes where it should.
It was an issue with the folder permissions not staying set to 755. I logged out of and back into cPanel and everything's working now.
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.
What I'm trying to do and achieve are:
I want to upload an image during the registration of a user. Let's say, I have a form that accepts input as name and input file upload(uploadify button) and a save button.
uploadify.php
$targetFolder = 'uploads';
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$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.';
}
}
Javascript
jQuery('#file_upload').uploadify({
'swf' : plug+'/uploadify-v3.1/uploadify.swf',
'uploader' : plug + '/uploadify-v3.1/uploadify.php'
// Put your options here
});
How will I do that?
There are 3 events which you can use for this:
http://www.uploadify.com/documentation/uploadify/onuploaderror/
http://www.uploadify.com/documentation/uploadify/onuploadsuccess/
http://www.uploadify.com/documentation/uploadify/onuploadcomplete/
I would suggest you use onuploaderror and change the return code of your php script to match so it only handles errors.