File upload in PHP not working? - php

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)){

Related

How do you make button save form data on a csv file and redirect you to a new page at the same time?

I am trying to make a button that saves form data on a file and redirects you afterwards, but although the save function works just fine, the redirection doesn't happen. I've tried only href and formaction so far. Any suggestions?
Paste bin : pastebin
Thank you for your time reading this !
the code:
<html>
<head></head>
<footer>
<form method="post">
<input type="radio" id="html" name="fav_language" value="private_number" checked="checked">
<label for="html">private_number</label><br>
<input type="radio" id="css" name="fav_language" value="clieant_number">
<label for="css">clieant_number</label><br>
<input type="text" id="fname" name="fname" maxlength="11" autocomplete="off" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*?)\..*/g, '$1');" required >
<button type="submit" name="BUY"><i class="arrow right"></i></button>
</form>
</footer>
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
}
?>
</body>
</html>
You can use PHP's header() function for this. Change the PHP part to this
<?php
$numurs = $_POST['fname'];
$Partners = $_POST['fav_language'];
if ($numurs){
$f = fopen('numurs.csv', 'w');
fputcsv($f, Array($numurs, $Partners));
fclose($f);
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
?>

PHP input file error, echo file name with Post method

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']) ;
}
?>

My $_FILES['file']['name'] is producing gibberish

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

Error when moving a file

I have an HTML web form where users can upload multiple files. I am having trouble with moving the files though.
HTML:
My HTML:
<form enctype="multipart/form-data" method="post" action="save.php">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<input type="file" name="uploads[]" multiple="multiple" />
<input type="submit" name="submit" value="submit"/>
</form>
Save.php:
<?php
foreach ($_FILES['uploads']['name'] as $file) {
$target= UPLOADPATH . $file;
move_uploaded_file($file, $target)
or die('error with moving the file');
$file= time() . $_FILES['uploads']['name'];
echo $file;
}
What could I be doing wrong?
You have to use $_FILES['uploads']['tmp_name'] instead of $_FILES['uploads']['name'] for moving the file. name is the original file name but tmp_name is the current uploaded file path.

php/jquery multiple file uploads

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.

Categories