Apologies if this is a duplicate question it just seems that every article i visit doesnt work for me.
I have a basic form which has two fields and a file.
<form action="make_announce.php" method="post" enctype="multipart/form-data" data-ajax="false">
<label>Enter Subject Line (500char max):
<input name="subject" type="text" id="subject" size="50"/></label>
<label>Announcement :
<textarea name="announce" cols="50" rows="10" id="announce"></textarea></label>
<label>Post Image (Leave Blank for NONE)<br>
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<input type="file" name="image" /><br /></label>
<div align="center">
<input type="submit" name="Submit" value="OK" />
</div>
</form>
And my PHP File minus most of the variables.
if (!empty($_FILES["image"])) {
$myFile = $_FILES["image"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
$sql="...
mysqli_query($con,$sql);
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
$sql="...
mysqli_query($con,$sql);
exit;
}
else
{
chmod(UPLOAD_DIR . $name, 0644);
$sql="...
mysqli_query($con,$sql);
exit;
}
}
mysqli_close($con);
Although this may not be the best way to do this it does work fine on a basic php web page. however when i put it into a jquery mobile web page it again works but doesnt seem to be posting the file im uploading. even after finding lots of articles telling me to add the data-ajax="false" to my form.
Any help would be very much appreciated.
Thanks in advance.
Turn off Ajax navigation using data-ajax="false" on your form definition because you can't upload files using Ajax.
Related
I'm building a website that has a form that allows users to upload files, but the form seems to be going to the wrong place. I told it to go to "upload.php", but instead it tries to go to "“upload.php", which I did not include in the form action. It doesn't look like I added any unicode characters in the script. Any reason why this might be happening?
Here's my form in index.html:
https://pastebin.com/7PETk5Sy
<!DOCTYPE html>
<html>
<head>
<title>Deeper</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div id="titlebar">
<img src="DeeperNetIcon.jpg"></img>
<h1>DeeperNet</h1>
Back to Deeper Homepage
<br/><br/>
<form action="view.php" method="post">Search for World by ID: <input type="text" name="world"> <input type="submit" value="Search"></form>
<br/>
</div>
<p color="white">Welcome to DeeperNet!</p>
<div id="upload">
<h1>Upload World to DeeperNet</h1>
<form action=“upload.php” method="post" enctype="multipart/form-data">
World File:
<br/><br/>
<input type="file" name="world">
<br/><br/>
World Name:
<br/>
<input type="text" name="name">
<br/>
Description:
<br/>
<input type="textbox" name="desc">
<br/><br/>
<input type="submit" value="Upload World">
</form>
<br/>
</div>
</body>
The index.html File has 2 Forms, the second form is the one I'm talking about.
Here's my upload.php Script:
https://pastebin.com/0FyuAX3Q
<?php
include("index.html");
if(!empty($_POST)) {
if(isset($_FILES["world"])) {
$world = $_FILES["world"];
$name = $world["name"];
$tmp_name = $world["tmp_name"];
$size = $world["size"];
$ext = explode('.', $name);
$ext = strtolower(end($ext));
$allowed = "deep";
if ($ext == $allowed) {
if ($size <= 300000) {
$id = uniqid('', true);
$destination = 'worlds/' . $id . '.' . $ext;
if(move_uploaded_file($tmp_name, $destination)) {
echo '<br/>World: "' . $name . '" uploaded to DeeperNet!';
echo “<br/>World ID: ” . $id;
$info = fopen('worlds/' . $id . '.txt', 'w');
fwrite($info, $_POST['name'] . "\r\n");
fwrite($info, $_POST['desc']);
fclose($info);
}
}
}
MAMP says I'm using PHP 7.0.8 if you're wondering.
Replace your code with this as form accepts this double quotes (") not this one (“)
<form action="upload.php" method="post" enctype="multipart/form-data">
I have a form.html
<form action="user.php" method="post" enctype="multipart/form-data" >
<input type="text" size="40" name="UserCallFile" >
<input type="file" name="filename"><br>
<input type="submit" value="Send"><br>
</form>
And user.php
<?php
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_FILES["filename"]["name"]);
} else {
echo("Error");
}
?>
I need that user could download file to server which name of this file he written on text input. Ho to do that?
I tried this way
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_POST["fileName"] . "." . "png");
But of course here "png" have to be logic with any file extension.
I hope you are understand the idea of problem.
Simply try to play with this..
$image = $_FILES["filename"]["tmp_name"];
$dir = "gallery/"; //adjust this correctly, watch for slashes depending on your website config
$newname = $image['name'];
if(!#copy($image, $dir . $newname))
{
echo 'error';
}
I have created a PHP signup form for visitors to fill and submit that asks for their basic information.
I am trying to accomplish the following two tasks;
Add Image/File Upload Field
Redirect them to a confirmation page
I have been unable to make it work. Below is what I have.
My Code
HTML Form
<form name="form1" method="post" action="signup.php">
Username: <input type="text" name="user">
<br>Email: <input type="text" name="mail">
<br>Experience: <select name="exp"> <option value="beginner">Beginner</option>
<option value="intermediate">Intermediate</option> <option value="advanced">Advanced</option>
</select><br> <input type="submit" name="Submit" value="Sign Up">
</form>
Signup.php
<?php
$username = $_POST['user'];
$email = $_POST['mail'];
$experience = $_POST['exp'];
//the data
$data = "$username | $email | $experience\n";
//open the file and choose the mode
$fh = fopen("users.txt", "a");
fwrite($fh, $data);
//close the file
fclose($fh);
print "User Submitted";
?>
It seems like you lack an input field in your HTML to begin with.
here's an example of a form for uploading files.
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
Once you've done that you're not quite there yet because your file is stored in a temporary folder, you will need to move the file to your uploads folder like so:
$target_file = "uploads/" . basename ($_FILES["fileToUpload"]["name"]);
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)
I hope this helps!
Ta add an upload you need to add enctype="multipart/form-data" to your form tag,then add the upload field. For the Email field change the type to HTML5 type="email", this will do a little validaation check that it is in the correct format. At the bottom of the php file add a location header if all went well. You could put it all in one file with an if statement at the top.You should also sanitize your inputs
this is a upload file script which will loop through all the data of a file and insert
if(isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['filename']['name']. "uploaded successfully." . "</h1>";
}
$csv_file=$_FILES['filename']['tmp_name'];
$type=$_FILES['filename']['type'];
$handle = fopen($csv_file, "r");
$i=0;
while (($data = fgetcsv($handle)) !== FALSE) {
if($i>0) {
$import="insert into `table_name`(col1,col2,col3,col4,col5,col6,col7)values('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."','".addslashes($data[4])."','$data[5]','$data[6]')";
mysql_query($import) or die(mysql_error());
}
$i=1;
}
echo "Success";
echo "<br>";
echo $_FILES['filename']['type'];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="" id="">
Choose File:<br>
<input name="filename" type="file" />
<input type="submit" name="submit" value="submit" />
</form>
Ok I am new to mysql and php array system. I need this to work with my simple file script I have made. Below is the code like I said it works find. But I can not figure out how to add the extra inputs...
if(isset($_FILES["file"])) {
if ($_FILES["file"]["error"] > 0) {
// Error
$up = 0;
} else {
$name = $_FILES["file"]["name"];
move_uploaded_file($_FILES["file"]["tmp_name"], UPLOADS."/$name");
$up = 1;
}
header('Location: upload.php?upload='.$up);
}
if(!$steam->isLoggedin()) {
echo "Sorry Guest, You need to login with steam to upload! <br /> \n \n";
} else {
// Check our return status to see if it worked or not
if(isset($_GET['upload'])) {
if($_GET['upload'] == 1) {
echo 'Uploaded succesfully';
} else {
echo 'Upload failed';
}
}
}
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<h1>Upload A ScreenShot</h1>
<em>Photo Name:</em><input name="name" type="text" id="name" />
<br />
<em>Size:</em><input name="size" type="text" id="size" />
<br />
<em>Category</em>
<select id="category" name="category">
<option>ScreenShots</option>
</select>
<br />
<em>Upload</em>:<input type="file" name="file" />
<br />
<input type="submit" name="Submit" value="Submit" />
</form>
The upload part works. But what I can not do is figure out how to add Size, Description and Category to the php code. I just do not know how to
set that part up and I do not know were to place the code. If anyone could help me I would really apricate it :).
$name = isset($_POST['name']); just returns true what you want to do with this line:
$filename = $name."_"."$type"."_".$rand."_".$_FILES['file']['name'];
and don't forget about enctype= multipart/form-data in html whithout it you could not save your file
The code looks like this:
html
<form action="contact.php" method="post" enctype="multipart/form-data" onsubmit="return Validare();">
<input type="text" name="nume" value="Nume" class="contact" id="Nume" onclick="if(this.value=='Nume')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Nume'" /><font color="red">*</font><br />
<input type="text" name="email" value="Email" class="contact" id="Email" onclick="if(this.value=='Email')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Email'" /><font color="red">*</font><br />
<input type="text" name="telefon" value="Telefon" class="contact" id="Telefon" onclick="if(this.value=='Telefon')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Telefon'" /><br />
<textarea name="mesaj" rows="10" class="contact" id="Mesaj" onclick="if(this.value=='Mesaj')this.value='';" onblur="if(this.value.replace(/^\s+|\s+$/g,'')=='')this.value='Mesaj'">Mesaj</textarea>
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="submit" value="Trimite" />
</form>
php
for($i=0; $i<3; $i++){
if($_FILES["file"]["size"][$i] > 0){
$rand = rand(10000, 99999);
$name = $rand.rand(10000, 99999).$_FILES["file"]["name"][$i];
$tmp_name = $_FILES["file"]["tmp_name"][$i];
$target_path_big = "http://biroutraduceri.net/fisiere/".$name;
move_uploaded_file($tmp_name, "fisiere/".$name);
}
}
javascript
<script>
function Validare(){
if(document.getElementById("Nume").value.replace(/^\s+|\s+$/g,'') == "" || document.getElementById("Nume").value.replace(/^\s+|\s+$/g,'') == "Nume"){
alert("Numele nu este valid!");
return false;
}
if(document.getElementById("Email").value.replace(/^\s+|\s+$/g,'') == "" || document.getElementById("Email").value.replace(/^\s+|\s+$/g,'') == "Email"){
alert("Email-ul nu este valid!");
return false;
}
if(document.getElementById("Mesaj").value.replace(/^\s+|\s+$/g,'') == "" || document.getElementById("Mesaj").value.replace(/^\s+|\s+$/g,'') == "Mesaj"){
alert("Mesajul nu este valid!");
return false;
}
return true;
}
</script>
When I press submit nothing happen. The file isn't uploaded.
Where I'm wrong???
Your PHP code has an error, the $tmp_name is never set.
Corrected code
for($i=0; $i<3; $i++){
if($_FILES["file"]["size"][$i] > 0){
$rand = rand(10000, 99999);
$name = $rand.rand(10000, 99999).$_FILES["file"]["name"][$i];
$target_path_big = "http://biroutraduceri.net/fisiere/".$name;
move_uploaded_file($_FILES["file"]["tmp_name"][$i], "fisiere/".$name);
}
}
$tmp_name is never initialized to anything.
$tmp_name should be set equal to $_FILES['file']['tmp_name'][$i];
Could be a permission problem, does your script have permission to write in "fisiere/".$name and is "fisiere/".$name really where you think it is? You might want to use an absolute path.
Edit: You cannot write an image to a http url, you need to write it to a local file-path and you need to make sure php has permissions to write to that path / directory
You realy dont need the onsubmit="return Validare();" and it is even written wrong.
and move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) is the correct code for
You forgot to set up the variable $tmp_name. As in $tmp_name = $_FILES["file"]["tmp_name"][$i];
Otherwise, it seems ok as per my own tests.
Otherwise, throw in a print_r($_FILES); before your "for" loop, a couple more prints and a is_readable($tmp_name) check inside your loop, just to more finely try to pin-point the source of the problem.
i guess onsubmit="return Validare();" is returning false
why are you using this rand function anyway, try time() it's better i think
you want $_FILES['file'][$i]['size']