Uploading files using Uplodify works sometime and sometime it does not - php

I installed Uploadify UI on my site so I can allow user to upload files to my site. It is working now but the problem is that it uploads files sometimes and sometimes it does not. So I can upload an png file and it will work no problems. but when I upload excel file it will allow some to be uploaded and some it work allow it.
I am not sure what can I check to see what is causing the issue.
I have tried to turn the debuger on 'debug' : true but that did not give me any clues. I also looked at my php logs and there was no error/warnings there.
I am not sure what else I can check but I will apreshiate any type of help with this.
Here is my javascript code to set it up
<?php $timestamp = time();?>
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5($timestamp);?>',
'upload_path': 'ticketing_center/',
'allowed_extentions': 'jpg,jpeg,gif,PNG,JPG,png,JPEG, jpeg,zip,rar,doc,docx,cvs,xls,xlsx,txt'
},
'auto' : true,
'debug' : true,
'swf' : '../../includes/uploadify.swf',
'uploader' : '../../includes/uploadify.php',
'fileSizeLimit' : '50MB',
'fileTypeExts' : '*.gif; *.jpg; *.JPG; *.png; *.PNG; *.JPEG; *.jpeg; *.zip; *.rar; *.doc; *.docx; *.cvs; *.xls; *.xlsx; *.txt;',
'onUploadSuccess' : function(file, data, response) {
if(data != 'INVALID'){
$('#attached_files').append('<input type="hidden" name="attachments[]" value="'+ $.trim(data) +'" />');
} else {
alert('Invalid File Type');
}
}
});
});
</script>
the following is my PHP script
<?php
$targetFolder = '';
$verifyToken = '100';
$actualToken = '';
$fileTypes = array('jpg','jpeg','gif','png');
if(isset($_POST['upload_path'])){
$targetFolder = $_POST['upload_path'];
}
if(isset($_POST['timestamp'])){
$verifyToken = md5($_POST['timestamp']);
}
if(isset($_POST['token'])){
$actualToken = $_POST['token'];
}
if(isset($_POST['allowed_extentions'])){
$types = explode(',', $_POST['allowed_extentions']);
if(count($types) > 0 ){
$fileTypes = $types;
}
}
if (!empty($_FILES) && $actualToken == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = ROOT_FIXED . UPLOAD_DIR . $targetFolder; //$_SERVER['DOCUMENT_ROOT']
$targetPath = str_replace( "//", "/", $targetPath);
$new_filename = USER_ID . '_' . time() . '_' . str_replace(" ", "_", $_FILES['Filedata']['name']);
$targetFile = $targetPath . $new_filename;
// Validate the file type
//$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($new_filename); //str_replace(" ", "_", $_FILES['Filedata']['name'])
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo trim($new_filename);
} else {
echo 'INVALID';
}
}
?>
Thanks for your time and help

I don't understand why people vote your questions down without a comment that will help you understand why. but any way to help other that will run into the same issue. Here is the solution
the configuration in my php.ini file were set to a low limites. Max upload file was set to 2M
go to php.ini and find the
upload_max_filesize - set to 50M
post_max_size - set to 50M
max_execution_time - set to 120
max_input_time - set to 50
Restart our apache and now you should be able to upload files up to 50MB in size

Related

Cannot pass values to uploadify using onuploadstart and formdata

When I add the formData part 'formData' : {'gallery_choice' : 'EMPTY'} the code still works, but I want to change 'EMPTY' to the chosen gallery with PHP using <?php $GLOBALS['gallery_choice'] ?> or whatever else works (but the value I need is saved as this global).
When I add the onUploadStart part (added in the code section below), the uploadify.swf file stops working and I'm presented with the fall back option (a boring HTML "Choose File" button) instead of the fancy flash button - what am I doing wrong?
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
//'auto' : false,
'formData' : {'gallery_choice' : 'EMPTY'},
'swf' : 'uploadify.swf',
'uploader' : 'uploadify.php',
'queueSizeLimit' : 10,
'width' : 150,
'height' : 20,
'buttonText' : 'Vælg Filer',
'checkExisting' : '/check-exists.php',
'fileTypeDesc' : 'Image Files',
'fileTypeExts' : '*.gif; *.jpg; *.png',
'fileSizeLimit' : '400KB',
//'uploadLimit' : 20
// Put your options here
'overrideEvents' : ['onSelectError','onDialogClose','onUploadStart'],
'onUploadStart' : function(file){
$('#file_upload').uploadify('settings','gallery_choice','TEMP'});
},
'onSelectError' : function(file, errorCode, errorMsg) {
switch(errorCode) {
case 110:
alert('ALERT! File is to big! The File: "' + file.name + '" is larger than the maximum size of 400KB! The file was not uploaded!');
break;
case 130:
alert('ALERT! INCORRECT FILE TYPE! The file: "' + file.name + '" is not of the allowed type (.jpg, .png eller .gif)! The file was not uploaded!');
break;
case 120:
alert('ALERT! THE FILE IS EMPTY! The file: "' + file.name + '" is empty (0 bytes)! The file was not uploaded!');
break;
default:
alert('ALERT! The file: "' + file.name + '" made an error with the error code: ' + errorCode + ', as well as the following message: ' + errorMsg + ' The file was not uploaded!!');
}
}
});
});
</script>
In case anyone wants it, here is also the uploadify.php file:
<?php
// Define a destination from user choice
if(isset($_GET['gallery_choice'])){
$targetFolder = 'uploads/' . $_GET['gallery_choice'] . '/';
}else
$targetFolder = 'uploads/TEMP/';
//$targetFolder = '/uploads'; // Relative to the root
//$targetFolder = 'uploads/';
//$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) /*&& $_POST['token'] == $verifyToken*/) {
//$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'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($_FILES["Filedata"]["tmp_name"], $targetFolder . $_FILES["Filedata"]["name"]);
echo '1';
} else {
echo '0';
}
/*
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
*/
}
?>
I've Googled and Stack-Overflowed, but not found a solution.
The problem I experienced with the Flash button not turning up was due to this line 'formData' : {'gallery_choice' : 'EMPTY'},
When I changed it to 'formData' : {'gallery_choice' : '<?php echo $GLOBALS['gallery_choice']; ?>'},
it started working and once I figured out to use $_POST instead of $_GET in my Uploadify.php, it started working - this is the update to Uploadify.php:
if(isset($_POST['gallery_choice'])){
$targetFolder = 'uploads/' . $_POST['gallery_choice'] . '/';
}else
$targetFolder = 'uploads/TEMP/';

I can't upload files using Uploadify onto my server

I am having problems when trying to upload files onto my server. I am using the uploadify plugin to upload files onto my server then store the filename in a database.
the problem is that uplodify acts like the file has been uploaded with no error but nothing is being uploaded.
I am runing PHP on a Windows 2008 R2 Server
the following is my php code that handle the actual upload.
<?php
require("../requires/LDAP_connection.php");
require("../requires/APP_configuration.php");
require("../requires/PHP_generic_functions.php");
//Include connection class
require('../classes/connection.php');
require('../requires/user_authentication.php'); //this file must be placed under the connection class NOT before
define('ROOT_SYS', dirname(__FILE__).'/');
// Define a destination
$targetFolder = '';
$verifyToken = '100';
$actualToken = '';
$fileTypes = array('jpg','jpeg','gif','png');
if(isset($_POST['upload_path'])){
$targetFolder = $_POST['upload_path'];
}
if(isset($_POST['timestamp'])){
$verifyToken = md5($_POST['timestamp']);
}
if(isset($_POST['token'])){
$actualToken = $_POST['token'];
}
if(isset($_POST['allowed_extentions'])){
$types = explode(',', $_POST['allowed_extentions']);
if(count($types) > 0 ){
$fileTypes = $types;
}
}
if (!empty($_FILES) && $actualToken == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = ROOT_SYS . $targetFolder; //$_SERVER['DOCUMENT_ROOT']
$new_filename = USER_ID . '_' . time() . '_' . str_replace(" ", "_", $_FILES['Filedata']['name']);
$targetFile = $targetPath . $new_filename;
// Validate the file type
//$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($new_filename); //str_replace(" ", "_", $_FILES['Filedata']['name'])
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo trim($new_filename);
} else {
echo 'INVALID';
}
}
?>
the following is my javascript
<?php $timestamp = time();?>
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5($timestamp);?>',
'upload_path': 'add-ons/ticketing_system/uploads/',
'allowed_extentions': 'jpg,jpeg,gif,PNG,JPG,png,zip,rar,doc,docx,cvs,xls,xlsx,txt'
},
'auto' : true,
'swf' : '../../includes/uploadify.swf',
'uploader' : '../../includes/uploadify.php',
'fileSizeLimit' : '10MB',
'fileTypeExts' : '*.gif; *.jpg; *.JPG; *.png; *.PNG *.zip; *.rar; *.doc; *.docx; *.cvs; *.xls; *.xlsx; *.txt;',
'onUploadSuccess' : function(file, data, response) {
if(data != 'INVALID'){
$('#attached_files').append('<input type="hidden" name="attachments[]" value="'+ $.trim(data) +'" />');
} else {
alert('Invalid File Type');
}
}
});
});
</script>
What am I doing wrong? Why is it not uploading anything and also not giving me any error?
Thanks
Uploadify has a debug mode to help you with issues like this : http://www.uploadify.com/documentation/uploadifive/debug-2/
The things I usually look for are permissions errors...and maybe your authentication methods.

Uploadify is not working

I am using uploadify to upload multiple files on my website, but it is not working properly. I have fix various things in it but it is not working properly. issue is that when i upload the file it will show me the loading bar and when it completes it disappear but when i check the destination the file is not present in it. Please help me. My code is given below:
index.php
<form>
<div id="queue"></div>
<input id="file_upload" name="file_upload" type="file" multiple="true">
</form>
<script type="text/javascript">
<?php $timestamp = time();?>
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'swf' : 'uploadify.swf',
'uploader' : 'uploadify.php'
});
});
</script>
uploadify.php
<?php
// Define a destination
$targetFolder = '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;
$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.';
}
}
?>
As said by some comments, when uploading using PHP, you need to give writing permissions to the upload directory. If you use filezilla, simply rightclick on the folder on the server, click fileattributes, and set it to, for example 775.
Had a similar problem just yesterday. My problem was that my upload_max_filesize, located in the php.ini, was set to 2M. I boosted it up to 4M and TADA, problem solved. If you do seem to be having the same issue, don't forget to restart your server. ;)

Uploadify FOLDER

I am using Uploadify to upload Files and using Codeigniter framework.
Here is my Uploadify code :
$("#change_profile_icon").uploadify({
'uploader' : '/project/style/scripts/crop/uploadify/uploadify.swf',
'script' : 'http://localhost/project/pages/profile_icon',
'cancelImg' : '/project/style/scripts/crop/uploadify/cancel.png',
'buttonText' :'Upload image',
'width' : '110',
'height' : '30',
'queueID' : 'fileQueue',
'auto' : true,
'scriptData' :{username :"<?php echo $this->session->userdata('username');?>",folder:"honda"},
'queueSizeLimit' : 1,
'multi' : false,
'fileDesc' : 'jpg',
'fileExt' : '*.jpg;*.png',
'sizeLimit' : '819200',//max size bytes - 800kb
'onComplete' : function(event,queueID,fileObj,response,data) {
alert("Completed");
var dataresponse = eval('(' + response + ')');
//$('#uploadifyUploader').remove();
var filenametmp = "http://localhost"+(dataresponse.file).substring(0,(dataresponse.file).lastIndexOf("?"));
var current_page = $('#page-list').val();
},
'onSelect' : function (){
var folder = $('#page-list option:selected').text(); //returns HONDA which is correct
$('#change_profile_icon').uploadifySettings('folder',folder);
} ,
'onError' : function(){
alert('error');
}
});
Here is my PHP part [script value in Uploadify]
function profile_icon()
{
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_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
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
// } else {
// echo 'Invalid file type.';
// }
}
Problem :
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
if (!file_exists($targetPath))
{
mkdir(str_replace('//','/',$targetPath), 0755, true);
}
Check the above codes taken from the PHP part.I think that $_REQUEST['folder'] will give the folder name which is specified on the Uploadify script.The value of folder is Honda
But this gives something different.
I uploaded a file and this script uploaded it to
C:\wamp\www\project\uploads\project\home\editpage\honda\honda
On wamp server [I am in Localhost]
But how it comes ?? it should be
C:\wamp\www\project\uploads\honda
Check the below...
$targetPath = 'uploads/' .$_REQUEST['folder']. '/';
$targetFile = $targetPath.$_FILES['Filedata']['name'];
The targetPath should be now uploads/honda/
and targetFile should be now uploads/honda/fileName.ext
I dont know what i am doing wrong and where it is....
Please help me.
Thank you .
EDIT : THE URL STRUCTURE OF CURRENT PAGE : http://localhost/Project/home/editpage/honda/
Where home is a controller and editpage is a function and honda is a argument.[Codeigniter framework]
SOLVED
I solved the issue,it is a bug in uploadify : The uploadify folder variable is not straight forward ,so that we should add a slash before that.
so it would be var folder = "/"+ "FolderName";
The problem is u cant return the data on server if u use just Folder name.
I solved the issue,it is a bug in uploadify : The uploadify folder variable is not straight forward ,so that we should add a slash before that.
so it would be var folder = "/"+ "FolderName"; The problem is u cant return the data on server if u use just Folder name.
I will think you will get a lot of help to put your output into a file, :)
$myFile = "[full-folder-path]testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_FILES['Filedata']['name'] ." | ". print_r( $_REQUEST, true ) ."\n";
fwrite($fh, $stringData);
fclose($fh);
But i think if you changes your line $targetPath to this
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
Remeber your folder need read/wirte rulls, its the only thing i can see its worng from your basic script to my script.
Just provide the folder where you want to upload in the settings as
<script type="text/javascript">
$(document).ready(function() {
$('#file_upload').uploadify({
'uploader' : '/uploadify/uploadify.swf',
'script' : '/uploadify/uploadify.php',
'cancelImg' : '/uploadify/cancel.png',
'folder' : '/uploads',
'auto' : true
});
});
</script>
As in the above settings you missed out the folder option.

How to get a unique name for each uploaded file using uploadify

I am uploading files using jQuery uploadify plugin. All files are uploaded into same directory. When I try to upload a file twice, it give me following error.
filename.gif (4.3KB) - IO Error
I want to upload a file with unique name every time. There are many other users uploading files in same directory. So there is a chance that two users share same file name. How can I avoid overwritten.
My Code:
$('.SingleFileUpload').uploadify({
'uploader' : '/uploadify/uploadify.swf',
'script' : '/uploadify/uploadify.php',
'cancelImg' : '/uploadify/cancel.png',
'folder' : '/uploads',
'auto' : true,
'queueID' : 'fileQueue',
'removeCompleted':false,
'onComplete' : function(event, ID, fileObj, response, data) {
$(event.target).closest('form').append( '<input type="hidden" name="uploaded_file" value="' + response + '">' );
}
});
To start off, reject duplicate file names:
$targetFolder = '/uploads'; // Relative to the root
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'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
if (file_exists($targetFile)){
echo 'File does already exist, choose another name!';
}
else {
move_uploaded_file($tempFile,$targetFile);
echo '1';
}
} else {
echo 'Invalid file type.';
}
}
?>
You can prefix all of your file names by the ID of the user + an underscore (or any character to separate the ID from the file name, to avoid UID1 + 2file == UID12 + file.
Instead of forcing the user to choose another name, you can also implement an automated name change: Either by adding a prefix and/or postfix, or by calculating the hash of the file. The last option also prevents duplicate files (same name, same contents) from appearing at the server.
I don't think using javascript is good or safe for this, one method I use is to name each file by calculate its SHA1 on server side.
I'm duplicating the filename and adding the copynr at the end. like:
filename(2)
filename(3)
filename(4) etc.
$('#upload').uploadify({
'uploader' : 'resources/plugins/uploadify/uploadify.swf',
'script' : 'resources/plugins/uploadify/uploadify.php',
'cancelImg' : 'resources/plugins/uploadify/cancel.png',
'folder' : 'uploads/',
'auto' : true,
'multi' : false,
'fileDesc' : '*.jpg;*.jpeg;*.png;*.gif;',
'fileExt' : '*.jpg;*.jpeg;*.png;*.gif;',
onComplete: function (evt, queueID, fileObj, response, data){
alert(response); // = uploaded filename
}
just the uploadify JS part (not using the check method!)
Now the uploadify.php part
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$ext = '.'.pathinfo($_FILES['Filedata']['name'], PATHINFO_EXTENSION);
$filename = substr($_FILES['Filedata']['name'],0,-strlen($ext));
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $filename . $ext;
while(file_exists($targetFile)){
preg_match('/\([0-9]{1,}\)$/', $filename,$matches);
if(!empty($matches)){
preg_match('/[0-9]{1,}/', $matches[0],$nr);
$filename = substr($filename,0,-strlen($matches[0])) . '('.(((int)$nr[0])+1).')';
}else
$filename .= '(2)';
$targetFile = str_replace('//','/',$targetPath) .$filename . $ext;
}
if(!is_dir($targetPath)) mkdir(str_replace('//','/',$targetPath), 0755, true);
move_uploaded_file($tempFile,$targetFile);
echo $filename.$ext;}
HTH

Categories