Uploadify shows completed but no file in directory - php

I am using uploadify for file uploading.
It's showing progress and completed but when I look into the directory there is no file uploaded
Here's my code:
index.php
<!DOCTYPE html>
<html>
<head>
<title>My Uploadify Implementation</title>
<link rel="stylesheet" type="text/css" href="uploadify/uploadify.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript">
$(function() {
$('#file_upload').uploadify({
'swf' : 'uploadify/uploadify.swf',
'uploader' : 'uploadify/uploadify.php'
// Your options here
});
});
</script>
</head>
<body>
<input type="file" name="file_upload" id="file_upload" />
</body>
</html>
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['file_upload']['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['file_upload']['name']);
if (in_array($fileParts['extension'],$fileTypes)) {
move_uploaded_file($tempFile,$targetFile);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
Here's a screenshot of my FTP directories

Related

how upload file from folder in php then deleted it after uploaded

I am trying to upload a file from one folder on my Pc to another folder on the XAMPP server. Then after uploaded the file must be deleted from the source.
I have written this code which uploads the file to the folder on the XAMPP server but does not remove it from the source and it shows the error "Unlink resource temporarily unavailable".
my code is :
<?php
if (isset($_POST['save'])) {
// destination of the file on the server
$destination = 'uploads/' . $filename;
$file = $_FILES['myfile']['tmp_name'];
$size = $_FILES['myfile']['size'];
$filepath = 'uploads/'.$filename ;
//-------------------------------
elseif (file_exists($filepath)) {
echo '<script type="text/javascript">';
echo ' alert(" Failed, Sanad already exist")';
echo '</script>';
} else {
if (move_uploaded_file($file, $destination)) {
$filename = $_FILES['myfile']['name'];
//$filename = $_POST['myfile'];
//$file_Path = 'uploads.$filename;
$file_Path = 'D:/pic/'.$filename;
echo $file_Path ;
// check if the file exist
if(file_exists($file_Path))
{ unset($filename);
unlink($file_Path);
echo 'File Deleted';
}else{
echo 'File Not Exist';
}
}
}
}
?>
<html>
<head>
</head>
<body >
<form action="tt.php" method="post" enctype="multipart/form-data" class= "form" >
<h3>Upload File</h3>
<input type="file" name="myfile" > <br>
<button type="submit" class ="add "name="save" value="save" class = "button"> save</button>
</form>
</body>
</html>

How do I show the resulting image after a php upload?

I'm trying to display the uploaded image and it's url after it has been processed using this code but I'm a bit stuck on how I would achieve this instead of the page returning with "done..."
http://llngg6czd-site.1tempurl.com/tester/index.php
Index.php
<!DOCTYPE html>
<html>
<head>
<title>Upload Files using normal form and PHP</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="upload_image.php">
<div class="row">
<label for="image">Select a File to Upload</label><br />
<input type="file" name="image" />
</div>
<div class="row">
<input type="submit" value="Upload" />
</div>
</form>
</body>
</html>
image_upload.php
<?php
require_once('ImageManipulator.php');
if ($_FILES['image']['error'] > 0) {
echo "Error: " . $_FILES['image']['error'] . "<br />";
} else {
// array of valid extensions
$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');
// get extension of the uploaded file
$fileExtension = strrchr($_FILES['image']['name'], ".");
// check if file Extension is on the list of allowed ones
if (in_array($fileExtension, $validExtensions)) {
$newNamePrefix = time() . '_';
$manipulator = new ImageManipulator($_FILES['image']['tmp_name']);
// resizing to 200x200
$newImage = $manipulator->resample(200, 200);
// saving file to uploads folder
$manipulator->save('uploads/' . $newNamePrefix . $_FILES['image']['name']);
echo 'Done ...';
} else {
echo 'You must upload an image...';
}
}
Imagemanipulator.php
https://gist.github.com/philBrown/880506
Any help would be greatly appreciated.
You save the file as
'uploads/' . $newNamePrefix . $_FILES['image']['name']
Therefor you can simply show it by giving back an IMG-Tag with the 'src' attribute:
echo '<img src="./uploads/' . $newNamePrefix . $_FILES['image']['name'] . '">';

PHP create folder inside ($_POST)

I need to upload the image to a server and store to the directory created by PHP using current timestamp. Below is the relevant part of the code, but not working as expected, it's not creating a folder as well it's not printing to the console. What could be the issue?
Edit:
Modified php based on below comment
upload.php
<?php
//get unique id
$up_id = uniqid();
?>
<?php
//process the forms and upload the files
if ($_POST) {
//specify folder for file upload
$user = "user";
//console.log("debug.......");
echo "debug.......";
if (!file_exists("/var/www/Scan")) {
mkdir("/var/www/Scan", 0777, true);
}
$folderName = date("Y-m-d") . "_" . date("h_i_sa") . "_" . $user;
if (!file_exists("/var/www/Scan/$folderName")) {
mkdir("/var/www/Scan/$folderName", 0777, true);
}
$folder = "/var/www/Scan/$folderName";
//specify redirect URL
$redirect = "upload.php?success";
//upload the file
move_uploaded_file($_FILES["file"]["tmp_name"], "$folder" . $_FILES["file"]["name"]);
//do whatever else needs to be done (insert information into database, etc...)
//redirect user
header('Location: '.$redirect); die;
}
//
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Upload your file</title>
<!--Progress Bar and iframe Styling-->
<link href="style_progress.css" rel="stylesheet" type="text/css" />
<!--Get jQuery-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.js" type="text/javascript"></script>
<!--display bar only if file is chosen-->
<script>
$(document).ready(function() {
//
//show the progress bar only if a file field was clicked
var show_bar = 0;
$('input[type="file"]').click(function(){
show_bar = 1;
});
//show iframe on form submit
$("#form1").submit(function(){
if (show_bar === 1) {
$('#upload_frame').show();
function set () {
$('#upload_frame').attr('src','upload_frame.php?up_id=<?php echo $up_id; ?>');
}
setTimeout(set);
}
//document.getElementById("message").innerHTML = "";
});
//
});
</script>
</head>
<body>
<div id="outPopUp">
<h1 >Upload your file </h1>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<br />
<br />
<!--Choose a file to upload<br />-->
<!--APC hidden field-->
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $up_id; ?>"/>
<!---->
<!-- <input name="file" type="file" id="file" size="30"/>-->
<label class="custom-file-upload">
<input name="file" type="file" id="file" onclick="myFunction()" />
Choose Video
</label>
<!--Include the iframe-->
<br />
<br />
<iframe id="upload_frame" name="upload_frame" color= black frameborder="0" border="0" src="" scrolling="no" scrollbar="no" > </iframe>
<br />
<!---->
<br />
<input class="btn btn-blue" name="Submit" type="submit" id="submit" value="Submit" />
<br />
<br />
<?php if (isset($_GET['success'])) { ?>
<span style="color:#FFFFFF;" id="message" class="notice">Your file has been uploaded.</span>
<?php } ?>
</form>
</div>
<script>
function myFunction() {
document.getElementById("message").innerHTML = "";
}
/*document.getElementById('file').onchange = function () {
//alert('Selected file: ' + this.value);
var path = this.value;
var fileName = path.replace(/.*(\/|\\)/, '');
alert('Selected file: ' + fileName);
//myFunction(fileName);
};*/
</script>
</body>
</html>
try a simple example for understanding:
<?php
//php content
if ($_POST) { //here we are checking $_POST values that $_POST has some values.
//specify folder for file upload
$tempDir = __DIR__ . DIRECTORY_SEPARATOR . 'upload'; //it means make a directory uploads where this php file is kept.
if (!file_exists($tempDir)) { // if $tempDir is not there, so it will create that directory.
mkdir($tempDir);
}
if(!empty($_FILES))//now checking your uploaded file is not empty
{
$nm=$_FILES['file']['name']; //here $nm get the name of the file
$tmp=$_FILES['file']['tmp_name'];//$tmp get the temporary file stored path
$mDir = __DIR__ . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR .time().$nm; //this your destination path with time+nameof file as a name of file.
if(move_uploaded_file($tmp,$mDir)) //uploading file to your destination path, if uploaded then it will go in the if scope and echo.
{
echo "file uploaded with timestamp in uploads folder";
//now redirect from here any where
}
else
{
echo "fail to upload a file";
}
}
}
?>

How to send a data from my form to uplodify.php

I'm trying to send my database row ID from this form to the uploadify.php so it can update that row with the file name but it's not happening when I run the same scrip with insert with out any ID the name get entered to the row below the needed data row. What should I do please help.
This is my form.php
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>UploadiFive Test</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="Upl/jquery.uploadify.min.js"></script>
<link rel="stylesheet" type="text/css" href="Upl/uploadify.css" />
<style type="text/css">
body {
font: 13px Arial, Helvetica, Sans-serif;
}
</style>
<?php
include ('lId.php')
?>
<script type="text/javascript">
$(function() {
$('#imgUpload').uploadify({
'auto' : false,
'swf' : 'Upl/uploadify.swf',
'uploader' : 'Upl/uploadify.php',
'height' : 20,
'width' : 200,
'fileTypeDesc' : 'Image Files',
'fileTypeExts' : '*.gif; *.jpg; *.png',
'method' : 'POST',
'scriptData' : {'iD' : "<?php echo $tId; ?>" },
// Put your options here
});
});
</script>
</head>
<body>
<h1>Uploadify Demo</h1>
<p>
<label for="poiid">ID :</label>
<input type="text" name="poiid" id="poiid" readonly="readonly" style="width:70px;" value="<?php echo $tId; ?>" />
</p>
<form enctype="multipart/form-data" method="POST">
<div id="queue"></div>
<input id="imgUpload" name="imgUpload" type="file" multiple="true" />
Upload Files
</form>
</body>
</html>
my upldify.php
<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/
include ('../../POIWeb/connect.php');
// Define a destination
$sId = $_REQUEST['iD'];
$path = 'POIWeb/img/';
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $path ; // 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 $fName;
echo '1';
} else {
echo 'Invalid file type.';
}
}*/
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetFile = $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);
$fName = $_FILES['Filedata']['name'];
//$imgPath = "INSERT INTO poiinfo(`Img`) VALUES ('$fName')";
$imgPath = "UPDATE poiinfo SET Img = '$fName' WHERE ID = '$sId'";
mysql_query($imgPath);
echo '1';
} else {
echo 'Invalid file type.';
}
}
?>
Use 'formData' instead of 'scriptData'. And get the data in $_POST array.
This code Upload Files is not needed
But uploadify uploads files directly - it means you need add next names of files, don't rewrite.

When I upload/copy an image it fails, when I hit refresh/resend it works

Alight so, I am working on a small section of a project and I am uploading an image and then copying it to resize afterwards. What is happening is that when I click submit to upload, it fails, but if I hit refresh/resend the info it succeeds...
$uploadFile = $uploadDir . $imageName;
$imageName2 = $front[0]."_large\.".$front[1];
$uploadFile2 = $uploadDir . $imageName2;
if(move_uploaded_file($imageTemp,$uploadFile))
{
if(!copy($uploadFile, $uploadFile2)) die("Can't copy $uploadFile2");
}
What it outputs when it fails is "Can't copy " So, for some reason it's not getting the name of the file to be copied to until I hit refresh?
Levi
Do you mean to escape the dot in $front[0]."_large\.".$front[1];
Were you thinking of a regular expression? if not this might be trying to save into a non existent directory.
Have you tried uploading another file, can you print_r() the $_FILES array, i've been stuck before figuring out why the $_FILES array is empty and there is no multipart form data in my form tag or the image has been corrupt and the upload stream cut by php.
Below working fine for me;
HTML File:
<!DOCTYPE html>
<html>
<title>Stack HTML</title>
<link rel="stylesheet" href="../../repo/css/bootstrap.css" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<head>
</head>
<body>
<div class="container">
<form method="post" action="resize.php" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="add" value="Add" />
</form>
</div>
</body>
</html>
resize.php
<?php
$uploadDir = 'uploads/';
$uploadLargeDir = 'uploads/large/';
$imageName = $_FILES['image']['name'];
$imageTemp = $_FILES['image']['tmp_name'];
$uploadFile = $uploadDir . $imageName;
if(move_uploaded_file($imageTemp,$uploadFile)) {
$front = explode('.', $imageName);
$imageName2 = $front[0]."_large.".$front[1];
$uploadFile2 = $uploadLargeDir . $imageName;
if(!copy($uploadFile, $uploadFile2)) {
die("Can't copy $uploadFile2");
} else {
die('Levi! Success');
}
}
?>

Categories