Help With Renaming Files With Uploadify - php

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.

Related

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?

Echoing File Name using PHP

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

Random File Name On Save Using Uploadify

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

Having trouble with explode() in uploadify.php

I have this snippet from my uploadify.php:
if (!empty($_FILES)) {
$name = $_FILES['Filedata']['name'];
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
$path = pathinfo($targetFile);
// this portion here will be true if and only if the file name of the uploaded file does not contain '.', except of course the dot(.) before the file extension
$count = 1;
list( $filename, $ext) = explode( '.', $name, );
$newTargetFile = $targetFolder . $filename . '.' . $ext;
while( file_exists( $newTargetFile)) {
$newTargetFile = $targetFolder . $filename . '(' . ++$count . ')' . '.' . $ext;
}
// Validate the file type
$fileTypes = array('pdf'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$newTargetFile);
echo $newTargetFile;
} else {
echo 'Invalid file type.';
}
return $newTargetFile;
}
Basically this is quite working. Uploading the file and getting the path of the file which will then be inserted on the database and so on. But, I tried uploading a file which file name looks like this,
filename.1.5.3.pdf
and when succesfully uploaded, the file name then became filename alone, without having the file extension and not to mention the file name is not complete. From what I understood, the problem lies on my explode(). It exploded the string having the delimiter '.' and then assigns it to the variables. What will I do to make the explode() cut the string into two where the first half is the filename and the second is the file extension? PLease help.
Don't use explode, use a function designed for the job: pathinfo()
$ext = pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);

Add time to the end of a uploaded file in Uplodify

Ok so I have been trying to get this to work and I dont see any errors but my syntax checker swears there is one on line 14. Can anyone help me out with this?
<?php
// Define a destination
$targetFolder = '***********'; // Relative to the root
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$tempFileExploded = explode($tempFile, ".");
//PROBLEM LINE
$tempFile = $tempFileExploded[0] . date('U') . $tempFileExploded[1];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];
//mkdir(str_replace('//','/',$targetPath), 0777, true);
// 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.';
}
}
//$targerfile is the file name
?>
The error im getting:
Parse error: syntax error, unexpected T_STRING in CODE on line 14
Errors parsing CODE
When you explode with dot. you need to extension with dot. so dot is missing in your code
$tempFile = $tempFileExploded[0] . date('U') .".". $tempFileExploded[1];

Categories