I want to view data stored in an uploading session but all I get is 'Null', am I going about this the wrong way?
session_start();
if(isset($_POST['submit'])){
$target = "test/";
$target = $target . basename('test') ;
$file = ($_FILES["uploaded"]["name"]);
$key = ini_get("session.upload_progress.prefix") . $_POST[ini_get("session.upload_progress.name")];
var_dump($_SESSION[$key]);
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){echo "done";}else echo "error";
}
and the html:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="test" />
<input type="file" name="uploaded" />
<input type="submit" name='submit' />
</form>
You're trying to get upload progress status when upload is already done.
If you want to make it working, then you can for example send your form to iframe and during the upload ask server, using ajax, what is the status.
I would suggest to use it rather as a fallback for older browsers cause currently browsers are supporting ajax upload and you can display upload progress without making additional requests to server and creating some strange hidden iframes ;)
Related
I am facing an issue with the below code, I can upload multiple files but still need that the if condition not be executed till user chooses a file as it is not good to see Unique ID and the echo message run however no files chosen!
PHP
<?php
$path = "Uploads/Files/";
if( (isset($_POST['submit'])) && (!empty ($_FILES['myFile']['name'])) ){
$countfiles = count($_FILES['myFile']['name']);
for($i=0;$i<$countfiles;$i++){
$filename = uniqid(). $_FILES['myFile']['name'][$i];
// Upload file
move_uploaded_file($_FILES['myFile']['tmp_name'][$i], $path.$filename);
echo "<h3> $filename has been uploaded Successfully!</h3>";
}
}
?>
So I want the && part above to be valid as well and not below message to appear till files be chosen.
HTML
<form method='post' enctype='multipart/form-data'>
<input type="file" name="myFile[]" id="file" multiple>
<input type='submit' name='submit' value='Upload'>
</form>
Error:
there are two parts:
add validation for required field <input type="file" name="myFile[]" id="file" multiple required>
render whatever you want instead of echo "<h3> $filename has been uploaded Successfully!</h3>";
I'm developing a website where multiple images are uploaded to a website and I'm playing around with ways that this can be done. At the moment I have a php script that will get & display images from a given folder which looks like this:
<?php
$dirname = "content/2014/February/";
$images = glob($dirname."*.*");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}
?>
This works fine and I can format the <img> using css and apply jquery for the gallery, BUT, how can I upload the folder of images using php and a html form in the first place?
It is possible to upload a folder now. You can get it done by following below code:
<input type="file" webkitdirectory mozdirectory />
You can check the demo here: https://jsfiddle.net/kevalpadia/vk6Ldzae/
I hope it will help you solve your issue.
<input type="file" webkitdirectory="" directory="" /> - this works just on few/modern browsers- like Edge or Webkit engines (Chrome). I think that is not supported by Firefox.
The Current Answer is NOT supported by all browsers.
You can not upload a whole folder.
currently only chrome supports it
And to upload many files http://www.uploadify.com/
You can use HTML5's
<input type="file" multiple>
About processing uploads in PHP, read more here:
http://php.net/manual/en/features.file-upload.post-method.php
Yes, It is possible.
Here is the code:
<form method="post" enctype="multipart/form-data" action="#">
Folder Name: <input type="text" name="foldername" /><br/>
Choose Directoryy: <input type="file" name="files[]" id="files" multiple directory="" webkitdirectory="" mozdirectory=""><br/>
<input class="button" type="submit" value="Upload" name="upload" />
</form>
<?php
if(isset($_POST['upload']))
{
if($_POST['foldername']!="")
{
$foldername=$_POST['foldername'];
if(!is_dir($foldername))
mkdir($foldername);
foreach($_FILES['files']['name'] as $i=>$name)
{
if(strlen($_FILES['files']['name'][$i]) > 1)
{
move_uploaded_file($_FILES['files']['tmp_name'][$i],$foldername.'/'.$name);
}
}
echo "Folder is uploaded successfully ..";
}
else
echo "Folder uploaded Failed!!";
}
?>
Give this PHP script a go:
Main website:
http://www.uploadify.com/
Documentation:
http://www.uploadify.com/documentation/
Below is my code where the user can upload a file. What I want to know is that is there a way so that via server side is there a way to first of all restrict the file formats of the files to jpeg and png only and then when the user clicks on the submit button, if the file format is correct then display an alert on the same page stating "File is correct" else display an alert stating "File is incorrect".
Can somebody please provide coding if they know how to do this. Thank you and any help will be much appreciated :)
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
A code for a total check of file uploads, you'll have to change $allowedtypes though. (Copied instead of linking because it was from a non-English site)
<?php
if(isset($_POST["submit"])){
$allowedtypes=array("jpg"=>true,"png"=>true,"gif"=>true,"txt"=>true);
$filename = $_FILES['file1']['name'];
$source = $_FILES['file1']['tmp_name'];
$file_size=$_FILES['file1']['size'];
$saveloc = "uploads/" . $filename;
$maxfilesize=1024*1024*10;
$nameext=explode(".",$filename);
if(preg_match('/^[A-Za-z0-9\-\_]{1,}\.[a-zA-Z0-9]{0,4}$/',$filename)){
if(!empty($allowedtypes[strtolower($nameext[1])]) && $allowedtypes[strtolower($nameext[1])]===true){
if($file_size<=$maxfilesize){
if(!file_exists($saveloc)){
if(move_uploaded_file($source, $saveloc)) {
chmod($saveloc,644);
echo "Successful upload. <a href='".$saveloc."'>Fájl megtekintése</a>";
}
else echo "Cannot move";
}
else echo "Existing file";
}
else echo "Too big file";
}
else echo "Not allowed extension";
}
else echo "Only alphanumeric files allowed";
}
else echo "<form method='post' enctype='multipart/form-data' action='secureupload.php'> File: <input type='file' name='file1' /><br /><input
name='MAX_FILE_SIZE' type='hidden' value='10485760' /> <input type='submit' value='Upload' name='submit' /></form>";
?>
You are talking about server side handler and write 'alert'...khm...
If u want to do stuff via server-side, then use php handler
http://php.net/manual/en/features.file-upload.post-method.php
If u want to do stuff via client-side, use javascript events, e.g on change event
<script>
function check() {
var file = document.getElementById('file').value;
var temp = file.split(/\.+/).pop();
alert(temp);
}
</script>
<input type="file" name="file" id="file" onchange="check();" />
You have file extension in temp var.
There are PHP functions to do this. You want to look at mime_content_type and finfo_file. These are built-in PHP commands that allow you to interpret that actual file type of a file being uploaded. You can then filter the mime types to only .gif/.jpg/etc. You want to check the mime types over the file name because the file name can be changed to mask the actual file type. If you want code samples, there are plenty on those pages as well as some excellent user-provided alternatives.
Something like this at the top of your file should work:
<?php
foreach ($_FILES as $file)
{
$tmp = explode(".", $file["tmp_name"]);
if (!in_array($tmp[count($tmp)-1], array("jpeg", "png"), true))
die("<script>alert('File is incorrect');</script>");
}
echo "<script>alert('File is correct');</script>";
?>
I am trying to find example code to upload files asyncronously (via Ajax) in IE8. Also upload progress would be nice, but not mandatory. Id like PHP code to be able to deal with the file server side. I keep coming across examples for other browsers using FormData, but I cannot use that. Could any body please point me in the right direction?
This is a good tutorial on the subject: http://hungred.com/how-to/tutorial-easiest-asynchronous-upload-file-ajax-upload/
HTML:
<form id="my_form" name="form" action="upload.php" method="POST"
enctype="multipart/form-data" >
<div id="main">
<input name="my_files" id="my_file" size="27" type="file" />
<input type="button" name="action" value="Upload" onclick="redirect()"/>
<iframe id='my_iframe' name='my_iframe' src="">
</iframe>
</div>
</form>
JS:
function redirect()
{
//'my_iframe' is the name of the iframe
document.getElementById('my_form').target = 'my_iframe';
document.getElementById('my_form').submit();
}
PHP:
$uploaddir = '/images/';
$uploadfile = $uploaddir . basename($_FILES['my_files']['name']);
if (move_uploaded_file($_FILES['my_files']['my_name'], $uploadfile)) {
echo "success";
} else {
echo "error";
}
That will get you started =)
User this http://jquery.malsup.com/form/#file-upload jquery plugin..Its best and tested..
Right now i'm using the below method to Upload a file to PHP
<form enctype="multipart/form-data" action="http://sserver.com/fileupload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000000" />
<input type="hidden" name="filename" value="file_uploaded.gif" />
<input type="hidden" name="username" value="foobar"/>
Please choose a file:
<input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
I read the $_POST and $_FILE in php to complete upload like this.
$target = $_SERVER['DOCUMENT_ROOT']."/test/upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
echo $target;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
My questions is , can i change the above said code (HTML) to an Ajax XMLHttpRequest without changes in PHP.
You cannot send files to the server via AJAX alone. This is because Javascript (when run in a browser) does not have access to the host's file system.
There are ways to make AJAX-style upload boxes using iframes, where the whole page is not reloaded during an upload, but this is not a simple task in itself. jQuery provides a couple of libraries to make this easier.
EDIT As ThiefMaster rightly points out HTML5 provides mechanisms for doing this more neatly.