How can I send errors caused in back-end PHP file of Uploadify to the uploading form? Right now when I have an error to report to the user from PHP I just echo and it goes to the onComplete method of Uploadify and alerts the user. Please see below:
<?php
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
}
if ($error) // I want to show this error on uploading form page.
{
echo "Some error";
}
else
{
echo '1';
}
?>
and I get the error like:
'onComplete': function(a, b, c, data, e){
alert(data);
}
Thanks
Have PHP echo 1 if success and an error message if not. Then just check the response back is 1 or something else.
'onComplete': function(a, b, c, data, e){
if (data == '1') {
alert('ok!');
}
else {
alert('Error: ' + data);
}
}
try this:
if ($error)
{
echo "error:".$error;
}
Uploadify has an OnError callback.
Like this:
'onError' : function(event, ID, fileObj, errorObj) {
alert(errorObj.type+"::"+errorObj.info);
}
This will only alert if there is an error with the upload. This specific code will let you know the error type and the information about the error
here is the link to the Uploadify documentation about this: onError
There is a guy who have fixed this problem, check the following link:
it helped me out big time!
From the Uploadify documentation:
Anything that is echoed in the uploadify.php script is accessible via the onUploadSuccess event as the second argument (data).
$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)) {
move_uploaded_file($tempFile,$targetFile);
echo $targetFolder . '/' . $_FILES['Filedata']['name'];
} else {
echo 'Invalid file type.';
}
}
$('#file_upload').uploadify({
// Some options
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
});