I am uploading files with the following code using Bootstrap as my front-end framework.
<form method="POST" enctype="multipart/form-data" action= "php/up-load.php" role="form"
<div class="form-group">
<label for="file" class="col-sm-5 control-label">
Select file(Compressed format)
</label>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000"/>
<input type="file" id="file" name="file" accept=".zip, .rar"/>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary" name="upload" id="upload">Send</button>
</div>
</form>
My php code is;
# $original=$_FILES['file']['name'];
# $kiss=pathinfo($original, PATHINFO_EXTENSION);
$allowed_extensions = array(".zip","rar","bzip2","iso","gz","rz","7z","tar","tgz","bz2","tbz2","lzma","tlz");
$result = in_array ("$kiss", $allowed_extensions);
if (!$result)
{
// Wrong file type
}
else
{
//proceed..
}
My problem is that the
$_FILES['file']['name'];
is returning gibberish such as php7vy8X9 and phpY8wQVR . Have tried everything. What could be the problem?
$_FILES["file"]["name"] should be returning the original name of your file, what you are reporting should be stored inside $_FILES["file"]["tmp_name"]
The whole $_FILES array should look like this:
echo $_FILES["file"]["name"]; // Original Name
echo $_FILES["file"]["tmp_name"]; // Name (and location) given to the file by PHP
echo $_FILES["file"]["type"]; // File/mime type
echo $_FILES["file"]["size"]; // Total bytes
echo $_FILES["file"]["error"]; // Any error code which is returned during transfer
Related
My file upload system just isn't working properly, it doesn't return true when I call move_uploaded_file(). Here is my code, not sure if i'm just blind:
HTML:
<form action="updatecheat.php" method="POST">
<label for="version">Version:</label>
<input type="text" class="form-control" id="version" name="version" placeholder="New Version Number" />
<input type="hidden" name="referrer" id="referrer" value="coven-updates.php" />
<div class="custom-file-upload">
<label for="covenupdate">Upload "Coven.exe"</label>
<input type="file" id="covenupdate" name="covenupdate" />
</div>
<br /><br />
<button type="submit" name="submit" id="submit" class="btn btn-success waves-effect waves-light m-r-10">Update</button>
</form>
PHP:
$errors= array();
$file_name = $_FILES['covenupdate']['name'];
$file_size =$_FILES['covenupdate']['size'];
$file_tmp =$_FILES['covenupdate']['tmp_name'];
$file_type=$_FILES['covenupdate']['type'];
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){
$fn = "../Coven/Utilities/Version.txt";
$file = fopen($fn, "w+");
$size = filesize($fn);
fwrite($file, $_POST['version']);
$text = fread($file, $size);
fclose($file);
header("Location: urlhere");
} else {
header("Location: urlhere");
}
I have no clue why it isn't uploading properly. Any help is appreciated!
Thanks!
With your form tag you are missing one attribute enctype='multipart/form-data' which is must while you are working with upload file.
So, just change your form tag to this:
<form action="updatecheat.php" method="POST" enctype='multipart/form-data'>
you need to include enctype for a file ulpload to work .i.e enctype="multipart/form-data"
Try with add attribute enctype='multipart/form-data' and
File will be stored in temporary location, use tmp_name instead of name
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){
I'm trying to take posted input file name, I'm not uploading anywhere.
I just need the name of posted filename so I'm trying this code;
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo $_FILES['file'];
}
?>
If I change enctype="multipart/form-data" into form tag, it's ok, but I need this tag.
You still need the enctype attribute, as the files will not be available without it.
if (isset($_POST['submit'])) {
echo $_FILES['file']['name'];
}
use
echo $_FILES['file']['name'];
instead of
echo $_FILES['file'];
$_FILES['file'] contains array of properties of uploaded file. use print_r instead. It will work fine.
you can get file name like that
$name = $_FILES['file']['name'];
this code is working fine
<form method="post" enctype="multipart/form-data" role="form">
<input type="file" id="file" name="file">
<input type="submit" name="submit" value="Submit Form">
</form>
<?php
if(isset($_POST['submit'])){
echo "<pre>";
print_r($_FILES['file']) ;
}
?>
All of my other form data is visible, but the name of the file is not showing up in the browser.
Here is a little portion of my code:
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
and this is the output:
Notice: Undefined index: file in D:\xamp\htdocs\colgWeb\index.php on line 228
problem
Use a validator: You misspelled enctype (it has a c in it).
Consequently, the form is being submitted with the default (url based) encoding which doesn't support file uploads.
You need to think that as a two step page. First, you send your form, then you use the input.
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="file" value="yoyo">
</form>
<?php
if (isset($_FILES["file"]))
{
$name = $_FILES["file"]["name"];
echo "File: $name";
}
?>
Please try below code. You are using same name (ie "file") for both file and submit button."
<form method="POST" action=<?php echo $_SERVER["PHP_SELF"];?> entype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit" value="yoyo">
</form>
<?php
echo $name = $_FILES["file"]["name"];
echo "problem";
?>
I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable $ _FILES
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>
I know there's quite a few topics on this already, and I've read quite a few of them, but to no avail. I have a form for uploading multiple files with descriptions. If the user needs more than one upload/description field, they can click a link to add another via jquery. The code for the form is:
<form action="adm.php?mode=upload" method="post">
<input type="hidden" name="article_id" value="<?php echo $article_id; ?>" />
<input type="hidden" name="new_path" value="<?php echo $new_path; ?>" />
<div id="files">
<div id="file" class="row">
<input type="file" name="file[]" id="file" /><br />
Description:<br />
<textarea name="desc[]" cols="50" rows="5"></textarea>
</div>
</div>
<div>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
Add File
A clone of
<div id="file" class="row">
<input type="file" name="file[]" id="file" /><br />
Description:<br />
<textarea name="desc[]" cols="50" rows="5"></textarea>
</div>
is appended to the files div when add_upload is clicked.
I believe this is correct usage of <input type="file" name="file[]" id="file" /> according to php.net, however the listener script at adm.php?mode=upload never receives the $_FILES array upon submission:
case 'upload' :
$path = request_var('new_path', '');
$article_id = request_var('article_id', 0);
$error = array();
$messages = array();
if(isset($_FILES['file']['tmp_name']))
{
// Number of uploaded files
$num_files = count($_FILES['file']['tmp_name']);
//create the directory if it doesn't exist already
if(!is_dir($path))
{
mkdir($path);
}
/** loop through the array of files ***/
for($i=0; $i < $num_files;$i++)
{
// check if there is a file in the array
if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
{
$messages[] = 'No file uploaded';
}
else
{
// copy the file to the specified dir
if(#copy($_FILES['file']['tmp_name'][$i],$path.'/'.$_FILES['file']['name'][$i]))
{
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
{
/*** an error message ***/
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
}
else
{
$messages[] = 'No files set for upload??';
}
$err_msg = $messages;
include($root_path.'pages/header.php');
include($root_path.'pages/error.php');
include($root_path.'pages/column.php');
include($root_path.'pages/footer.php');
exit;
break;
I always get the "No files set for upload??" error because the files aren't being transferred. All of the other values are sent over POST and received with no problem. What am I doing wrong?
Your form declaration in HTML needs to have the enctype property set.
<form enctype="multipart/form-data">
Then use something like livehttpheaders for firefox to see if the data is actually going across. After you add that enctype, there should be something in the $_FILES array on the php side.