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
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 have a form where i want to upload files with multiple inputs.My form looks like:
<form action="" method="post">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
I do not know how to process this form..
You form doesn't work until you don't include 'enctype="multipart/form-data"', because it is necessary to use input type file.
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
Now browse the file and submit the form. You will get all file data inside $_FILES. so to check what you get inside the file data, you can use :
echo '<pre>';
print_r($_FILES)
I'm not sure whether you have gone through the tutorials before, however below is the code which will help you to process it.
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
If you upload the file, you can get the files from,
$_FILES global array, i.e. $_FILES['tax'] and $_FILES['ta'].
More info can be found on php.net
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="tax" />
<input type="file" name="ta" />
<input type="submit" name="submit" />
</form>
<?php
//print_r($_POST);
if(isset($_POST['submit'])){
$name = $_FILES['tax']['name'];
$name1 = $_FILES['ta']['name'];
$temp_name = $_FILES['tax']['tmp_name'];
$temp_name1 = $_FILES['ta']['tmp_name'];
var_dump($_FILES);
if(isset($name)){
if(!empty($name)){
var_dump($_FILES);
$location = 'images/'.$name;
if(move_uploaded_file($temp_name, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
if(isset($name1)){
if(!empty($name1)){
var_dump($_FILES);
$location = 'images/'.$name1;
if(move_uploaded_file($temp_name1, $location)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
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">
My program is supposed to create a folder for the uploaded images on the directory but gives this warning:
mkdir() [function.mkdir]: File exists in C:\XAMP\xampp\htdocs\gallery\uploader3.php on line 26
Here is the code:
<html>
<head>
<title> Sample1 - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form action="uploader3.php" method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Create an Album (limited to 10 images): <br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<br />
<input type="submit" value="Upload File" />
</form>
</div>
<?php
$target_path = "uploads1/";
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
else
{
for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
{
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'][$count]).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
</body>
</html>
Modify your codes below:
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
to:
if(!file_exist($target_path)) {
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
}
That will check the folder first, if it's already exist, no need to create it again.
for your 2nd question, you need to store the uploaded image names to somewhere ( i guess DB is a good choice), Then, you can show them anywhere you want.
Or you can use below codes to search in folder and display them:
$image_files = glob("uploads1/*.jpg");
foreach($image_files as $img) {
echo "<img src='".$img."' /><br/>";
}
You should check first that the directory does not already exist before attempting to create it
if (!file_exists($target_path))
mkdir($target_path);
if (file_exists($target_path))
{
// Further processing here
}
else
{
// Could not create directory
}