PHP file upload not working - php

I'm trying to upload files to my server but it doesn't work at all. Here is test code:
<?php
echo count($_FILES['upload']['name']);
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>
It always prints 0, file upload is enabled on my server.

The problem is that you're not counting the $_FILES['upload'].
Simple fix for your problem
Use:
echo count($_FILES['upload']);
Instead of:
echo count($_FILES['upload']['name']);
Edit:
Remove [] from the input's name.

<?php
echo count($_FILES['upload']);//only this modified//
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input name="upload[]" type="file" accept=".mp3" multiple="multiple" />
<br>
<input type="submit" value="Upload">
</form>
</body>
</html>

Related

Input from form to url?

So the goal is to take the input from a form append it to the end of the URL and then return the HTML from that page.
I am not entirely sure how to take the forms value (in this case $2) and attach it to the URL.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$2")?>
<?php echo $_POST ["$html"];?>
</body>
</html>
On submit any input from a form with method="POST" will be stored in the $_POST global. You can access it from there and append it to your URL string.
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="iacoCode" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST["iacoCode"])) {
$html = file_get_contents("http://www.metar.mysite.net/metar?id=" . $_POST["iacoCode"]);
echo $html;
}
?>
</body>
</html>
Using the IF statement to check if it is set will prevent it from loading the URL with no variable.
The way we extract data via php from a form is like below
<html>
<head>
<title>Metar Test</title>
</head>
<body>
<form action="" method="POST">
<p>IACO Code: <input type="text" name="$2" value=""></p>
<input type="submit" name="submit" value="Submit">
</form>
<?php $html=file_get_contents("http://www.metar.mysite.net/metar?id=$_POST['$2']")?>
<?php echo $_POST ["$html"];?>

html/php not uploading files

I have looked all over stackoverflow and have not yet found a working answer.
The HTML form allows the user to upload multiple files using one single input, the values then travel to a php file (named upload.php) to be uploaded to their final resting place.... this doesn't happen.
My HTML form:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
My PHP file:
<?php
if(count($_FILES['upload']['name'])) {
foreach ($_FILES['upload']['name'] as $file) {
move_uploaded_file($_FILES["upload"]["tmp_name"], './uploads/'.$_FILES["upload"]["name"]);
}
}
?>
try this code
<form method="post" action="upload.php" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" name="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
Your PHP file
<?php
if(isset($_POST["submit"]))
{
if(count($_FILES['upload']['name'])) {
foreach ($_FILES['upload']['name'] as $key=>$file) {
move_uploaded_file($_FILES['upload']['tmp_name'][$key], './uploads/'.$file);
}
}
}
?>
I have edited this answer and below code is working for me.... hope this will help.
<!DOCTYPE HTML>
<html>
<body>
<form method="post" action="" enctype="multipart/form-data">
<input name="upload[]" type="file" multiple="multiple" />
<input type="submit" name="submit" value="Upload Files" class="btn btn-default btn-sm"/>
</form>
<div>
<?php if(isset($_POST['submit'])){
foreach($_FILES['upload']['name'] as $key=>$filename){
move_uploaded_file($_FILES["upload"]["tmp_name"][$key], '.pathtoupload/'.$filename);
}
}?>
</div>
</body>
</html>

How do you get user inpup and display it in php

I am trying to get user input from a text box and then echo it using php. Here is my code, and it is not seeming to work.
<html>
<body>
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<?php
if(!empty($_POST['value']))
{
echo filter_var($_POST['value'], FILTER_SANITIZE_STRING);}
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
</body>
</html>
First check if form posted.

submit form does not pass data to PHP

The submit form does not seem to pass along the uploaded file. The code is supposed to display "array" when a file is uploaded. Nothing happens when submit is pressed
<?php
$conn = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db ('coop',$conn);
if(isset($_POST['submit']))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
while(($fileop = fgetcsv($handle,1000,"|")) !== false)
{
echo $fileop;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled</title>
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
<br />
<input type="submit" name="submit" value="submit">
</body>
</html>
Your <form> </form> tag should wrap all elements of the form. Like the following:
<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="file" />
<br />
<input type="submit" name="submit" value="submit">
</form>
Your submit is outside of the form tags, Fix your HTML and the posting should work.

how to pass php variable value to action attribute of html form

I want to pass php variable value as a action to html form. I am trying as follows, but it is not working.
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
All this code are in one php file.
Have you tried <?php echo $url ?>
If it works, then short_open_tag in the php.ini is turned off. That means you will need to either turn it on or use the long open tag <?php throughout your code.
Sounds like you need to enable short_open_tag if your example doesn't work.
<?php
ini_set('short_open_tag', 'on');
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?=$url?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Alternately, write it like this:
<?php
$url='test.php';
?>
<html>
<body>
<form name="upload" action="<?php echo $url ?>" method="post" >
<input type="submit" value="submit">
</form>
</body>
</html>
Try this
<form name="upload" action="<? echo $url ?>" method="post" >
Remove your single quotes:
<form name="upload" action="<?=$url?>" method="post">

Categories