I have file upload and text input fields in my form and what I want is to show the error message for file upload on same page and don't go to the next page if there is error...if there is error on file upload return also the values of text fields to the same page... How I do it?
<form action="send.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" /><br>
<label for="file">Choose Photo:</label>
<input type="file" name="file" required><br>
First Name:<input type="text" name="fname" required><br>
Last Name:<input type="text" name="lname" required><br>
Choose Username:<input type="text" name="username" required><br>
Age:<input type="text" name="age" required><br>
<input type="submit" value="Submit" name="submit" >
</form>
</body>
</html>
Here is the php code for file upload....I will be inserting the data from <input type="text"> to database and I don't have the code for it now...
<?php
ini_set( "display_errors", 0);
if(isset($_REQUEST['submited'])) {
// your save code goes here
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo "<font color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "<font color='green'><b> Success! Your photo has been uploaded.</b></font>";
}
}
}
else
{
echo "<font color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}
?>
Just get your PHP to regenerate the form with the fields filled in (i.e. sst the value attribute) in the event of an error.
Heey,
To keep it on the same page u can remove the action
send.php
And then add the html and php code showed here in the same file. When that file is executed it will stay on the same page.
<?php
if (!empty($_POST)){
//send.php code
}
?>
<form action="send.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" /><br>
<label for="file">Choose Photo:</label>
<input type="file" name="file" required><br>
First Name:<input type="text" name="fname" required><br>
Last Name:<input type="text" name="lname" required><br>
Choose Username:<input type="text" name="username" required><br>
Age:<input type="text" name="age" required><br>
<input type="submit" value="Submit" name="submit" >
</form>
Related
I don't have an idea why it doesn't upload pictures(But the same peace of code does). Please help me.
HTML:
$max_filesize = 524288;
$upload_path = './img/';
$id=$_POST["id"];
for ($i=1; $i<=3; $i++)
{
(string)$inside="inside".$i;
echo $inside;
$filename = $_FILES[$inside]['name'];
echo $filename;
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
echo $ext;
if (isset($_POST[$inside])){
if(filesize($_FILES[$inside]['tmp_name']) > $max_filesize)
die('File is too big.');
if(!is_writable($upload_path))
die('Cannot access folder - 777.');
if(move_uploaded_file($_FILES[$inside]['tmp_name'],$upload_path . $id . "inside" . $i . $ext))
{
echo 'Uploaded successfuly ';
echo '<br><br>';
echo '<img src="' . $upload_path . $filename . '" width="300" >';
} else {
echo 'Something went wrong, try again.';
}
}
}
Before that:
<form method="post" enctype="multipart/form-data" action=""/>
Що в коробці(Фото): <p>
<input type="file" name="inside1"
accept=".jpg, .jpeg, .png"> <p>
<input type="file" name="inside2"
accept=".jpg, .jpeg, .png"> <p>
<input type="file" name="inside3"
accept=".jpg, .jpeg, .png"> <p>
</form>
It says 'Something went wrong, try again.'.
Strange form - <form></form>
<form action="<?php echo'admin/photo_upload.php'; ?>" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
<p><input name="file_upload[]" type="file" id="file_upload[]" value=""></p>
<p><input name="file_upload[]" type="file" id="file_upload[]" value=""></p>
<p>Caption: <input type="text" name="caption" value="" /></p>
<input type="submit" name="submit" value="Upload" />
I'm programming a script that uploads mp3 to a folder in my server and adds the title, url and artist tot the 'audio' table.
but it dont work
PHP MySQL multiple mp3 upload in database
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
for($i=0;$i<count($_FILES["filUpload"]["name"]);$i++)
$target_dir = "/mounted-storage/home150/sub007/sc80538-VHHY/website.com/audio/files/";
$trackName = $_FILES['foto']['name'];
$titel = htmlspecialchars($_POST['titel']);
$artist = $_POST['artiest'];
{
if($_FILES["filUpload"]["name"][$i] != "")
{
if(move_uploaded_file($_FILES["filUpload"]["tmp_name"][$i],"myfile/".$_FILES["filUpload"]["name"][$i]))
{
//*** Insert Record ***//
mysql_query("INSERT INTO `audio` (titel, url, categorie) values ('".$titel."', 'http://website.com/audio/files/".$trackName."','".$artist."')");
}
}
}
echo "Copy/Upload Complete<br>"; ?>
and the HTML form :
<form name="form1" method="post" action="upload.php" enctype="multipart/form-data">
Track1 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track2 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track3 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track4 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track5 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
Track6 : <input name="titel" type="text" id="Titel" size="63" /><p>
<input type="file" name="filUpload[]"><br>
<br><br>
<select name="artiest">
<?php
$query = mysql_query("SELECT * FROM artiesten ORDER BY naam ASC");
while ($array = mysql_fetch_assoc($query)){
echo "<option value=\"". $array['naam'] ."\">". $array['naam']. "</option>";
}
?>
</select>
<input name="btnSubmit" type="submit" value="Submit">
</form>
this is just a sample code you have to fit in your website and add mysql connection ware i leave comment or just include external file
php code
if (isset($_POST["upload"])){
$userdir = "mp3/songs/";
for($i=0; $i<count($_FILES['mp3']['name']); $i++) {
$target_file = $userdir . basename($_FILES["mp3"]["name"][$i]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (!file_exists($userdir)){
mkdir($userdir, 0777, true);
}
if (move_uploaded_file($_FILES["mp3"]["tmp_name"][$i], $target_file)) {
// your mysql connect code or you have to be included external file with $connect variable or just rename it
$song = $_FILES["mp3"]["name"][0];
$sql = sprintf(
"INSERT INTO music (mp3) VALUES ('%s')",
mysqli_escape_string($connect, $song)
);
$res = mysqli_query($connect,$sql);
echo "The file has been uploaded.\n";
} else {
echo "Sorry, there was an error uploading your file.\n";
}
}
}
html code :
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="mp3[]" multiple="true" >
<input type="submit" name="upload" value="upload">
</form>
I need to upload a file in a folder but the file never get in the folder. The name of the file is added correctly but no file in the folder. What is going wrong?
Php error: Undefined index: foto on line 12-16
$name= $_FILES["foto"]["name"];
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
$temp= $_FILES["foto"]["temp_name"];
$error= $_FILES["foto"]["error"];
if ($error > 0)
die("Error uploading file! code $error.");
else
{
if($type=="image/png" || $size > 2000000)//condition for the file
{
die("Format not allowed or file size too big!");
}
else
{
move_uploaded_file($temp,"assets/");
echo "Upload complete!";
}
}
HTML:
<form id="form" style="margin-left: 200px" action="addNewProduct.php" method="post"><br>
<br>
<br>
<div id="imageUpload">
<label for="foto">Foto</label>
<input type="file" name="foto" /><br>
</div>
<div id="infoForm">
<label for="productNaam">Productnaam</label>
<input type="text" name="productNaam"/><br>
<br>
<label for="beschrijving">Productbeschrijving</label>
<input type="text" name="productBeschrijving" /><br>
<br>
<label for="btw">BTW</label>
<input type="number" name="productBtw" /><br>
<br>
<label for="prijsinclbtw">PrijsInclBTW</label>
<input type="number" name="productPrijsInclBtw" /><br>
<br>
<br>
<br>
<input type="submit" name="submit" value="Add new Pen">
</div>
</form>
Your $_FILES["foto"]["temp_name"]; is incorrect, you should change it to $_FILES["foto"]["tmp_name"];
And your folder assets should exist in your root folder.
Your form should like this:
<form action="uploads.php" method="post" enctype="multipart/form-data">
<input type="file" name="foto">
<input type="submit" name="submit">
</form>
Please try this, hope this help you out : uploads.php
$type= $_FILES["foto"]["type"];
$size= $_FILES["foto"]["size"];
// $temp= $_FILES["foto"]["temp_name"];
$file_name = $_FILES["foto"]["name"];
$source = $_FILES['foto']['tmp_name'];
$dir = "./assets/";
$file = $dir . $file_name;
//$directory = "./assets/upload/$file_name";
if(!file_exists ($file ))
{
move_uploaded_file($source,$file );
exit();
}
else
{
echo "File exist";
}
When you upload files, form has to have enctype attribute.
<form enctype="multipart/form-data" id="form" style="margin-left: 200px" action="addNewProduct.php" method="post">
^^
I've put together an HTML Form for a PHP File Uploader but when I use the form and attempt to upload the file I get sent to a "Page not Found" and the file never ends up where it's supposed to on my server.
Here's the Form:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<p><font size="2">*=Required</font></p>
<b>Mod Name:*</b><br /><input type="text" name="modname" size="30"><br />
<b>Author:*</b><br /><input type="text" name="author" size="30"><br />
<b>Version:*</b><br /><input type="text" name="version" size="30"><br /><br />
<b>Game:</b><select>
<option value="morrowind">Morrowind</option>
<option value="oblivion">Oblivion</option>
<option value="skyrim">Skyrim</option>
</select><br /><br />
<p><b><font size="3">Dependencies:</font></b></p>
<p>Morrowind</p>
<input type="checkbox" value="expansion" name="tribunal" />Tribunal <br />
<input type="checkbox" value="expansion" name="bloodmoon" />Bloodmoon <br />
<input type="checkbox" value="mod" name="mge" />MGE <br />
<input type="checkbox" value="mod" name="mwe" />MWE <br />
<input type="checkbox" value="mod" name="mwse" />MWSE <br />
<p>Oblivion</p>
<input type="checkbox" value="expansion" name="knights" />Knights of the Nine <br />
<input type="checkbox" value="expansion" name="isle" />Shivering Isles <br />
<p>Skyrim</p>
<input type="checkbox" value="expansion" name="dawnguard" />Dawnguard <br /><br />
<label for="description"><b>Description:</b></label><br />
<textarea id="description" rows="4" cols="50"></textarea><br /><br />
<b>Uploader Name:</b><br /><input type="text" name="upname" size="30"><br /><br />
<label><b>Uploader Comments</b></label><br />
<textarea id="uploader" rows="4" cols="50"></textarea><br /><br />
<b>Mirror Links:</b><br /><input type="text" name="mirror" size="50"><br /><br />
<label for="file"><b>Upload Mod:</b></label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
<p><font size="2">Maximum File Size: 400MB; Only Compressed Files Accepted (.zip, .rar, .7z)</font></p>
</form>
and here's the upload php file:
<?php
$allowedExts = array("zip, rar, 7z");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "application/zip")
|| ($_FILES["file"]["type"] == "application/x-rar-compressed")
|| ($_FILES["file"]["type"] == "application/x-7z-compressed"))
&& ($_FILES["file"]["size"] < 419430401)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 400) . " Mb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
if (file_exists("uploads/mods/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/mods/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/mods/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Where did I mess up?
Do I need to place the .php file inside of the upload directory? Where does the form information go? Also, how do I require some of the text boxes in the form?
Thanks, I'm a bit lost here.
You can check the web server logs (like suggested before) to see if they give you an idea (usually they do). Without knowing your file/directory structure, it's pretty much guesswork. I noticed the form references upload_file.php, then you call the second code upload php. Is it just misnamed?
Also, make sure it is in the same directory as the HTML file with your form.
If you want the upload_file.php file to be in a subdirectory, you can do this by changing:
<form action="upload_file.php" method="post" enctype="multipart/form-data">
to:
<form action="subdirectory/upload_file.php" method="post" enctype="multipart/form-data">
I've searched around but I can't find my issue. I have a simple script that uploads a file to the target-folder.
$target = "img/fotos-artikels/";
$target .= basename($_FILES["uploadBron"]["name"]);
if(move_uploaded_file($_FILES["uploadBron"]["tmpname"], $target)) {
echo "The file " . basename($_FILES["uploadBron"]["name"]) . " has been uploaded.";
} else {
echo " Sorry there was a problem";
}
And this is the form:
<form enctype="multipart/form-data" action="" method="post">
<label for="txtBronNaam">Naam:</label>
<input type="text" name="txtBronNaam" id="txtBronNaam" value="" /><br />
<label for="uploadBron">File:</label>
<input type="file" name="uploadBron" id="uploadBron" value="" /><br />
<input type="submit" name="" value="Voeg bron toe" />
</form>
Do I have to enable something in apache maybe?
tmpname should be tmp_name