I am making a simple image uploader.
I want it so:
An image is uploaded, it calls a php script.
The page is then refreshed
I am using a simple html form action. It does not refresh the web page after submission. To prevent the php script from overriding the web page, I put it in a hidden iframe.
I have never used javascript or ajax, so I am trying to avoid those solutions. This is a one-off for fun thing, so investing time into them isn't my goal.
<body>
<iframe name="votar" style="display:none;"></iframe>
<p class="standard_text_big" style="text-align:center"> No Pin Limit Will Stop Us!</p>
<p><form action="upload.php" method="post" target="votar" enctype="multipart/form-data" class="standard_text" style="text-align:center">
Upload a File:
<input type="file" name="the_file" id="fileToUpload">
<input type="submit" name="submit" value="Start Upload">
</form>
</p>
<div class = "image_format">
<picture class = "image_format">
<div id="img_embed"></div>
</div>
</picture>
</body>
EDIT Solved:
Instead of doing this in the html page, I have removed the hidden iframe so the php script does take over.
Then in the php script, after it executes, it sends you back to the html page.
Like so:
<p class="standard_text_big" style="text-align:center"> No Pin Limit Will Stop Us!</p>
<p><form action="upload.php" method="post" enctype="multipart/form-data" class="standard_text" style="text-align:center">
Upload a File:
<input type="file" name="the_file" id="fileToUpload">
<input type="submit" name="submit" value="Start Upload">
</form>
</p>
<div class = "image_format">
<picture class = "image_format">
<div id="img_embed"></div>
</div>
</picture>
Then in the php file:
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been uploaded";
$command = escapeshellcmd('python3 /home/myfile.py');
$output = shell_exec($command);
echo $output;
header("Location: https://www.google.com"); /* Redirect browser */
exit();
Related
I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>
I have recently been working on a project that uses a bit of PHP. I don't know whether my question is obvious to those who have loads of experience, but here goes.
I don't know how to get a response from an upload PHP I created. If I have a form, like so...
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label>Select image to upload:</label>
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
...and I have a PHP script that uploads these images to Cloudinary...
<?php
// Cloudinary init
require 'Cloudinary.php';
require 'Uploader.php';
require 'Api.php';
\Cloudinary::config(array(
"cloud_name" => "(cloud name)",
"api_key" => "(my key)",
"api_secret" => "(secret here)"
));
// Uploads images to Cloudinary
\Cloudinary\Uploader::upload($_FILES["fileToUpload"]["tmp_name"]);
?>
...how can I make it so that when submitted, it adds the value of the photo's URL (which is stored in a hash) to a hidden input in another form? Thanks so much.
P.S. Sorry for asking such n00b-y questions! (I'm new here)
First, make sure the session has been started. This allows you to send data over the server with post and files.
session_start()
Then you can access your inputs by name using the post and file functions.
# form.php
<?php session_start() ?>
<form action="after_form.php" method="POST" enctype="multipart/form-data">
<input type="text" name="text-input" value="Moon Text" />
<input type="hidden" name="some-input" value="xyyz" />
<input type="submit" />
</form>
Then on your next page, once the form has been submitted,
# after_form.php
echo $_POST['text-input']; //prints "Moon Text"
echo $_POST['some-input']; //prints "xyyz"
Usually you would save these data somewhere though.
If on this same after_form.php page you have a new hidden input, you could do something like
<input type="hidden" name="file-path" <?php echo 'value="'.$_POST['data'].'"';?> />
I'm working on a web application in php that allows users to upload files with a specified structure (file type could be cvs or excel sheets) and the app will extract data from the files and insert them into a database.
I can't get my app to upload files and I've been trying for 2 days, I checked the php.ini for max upload size ,max post size , file uploads on, temp directory set to /tmp which is accessible by all users.
I also checked the syntax of php and html and made sure I was using the right encoding type, I also checked the permissions of the upload directory but the _FILES variable is always empty, note : my web server is hosted on amazon ec2 running Ubuntu 14.04 LTS.
here's one of the codes I tried and it's output:
<?php
echo $_FILES['file']['error'];
print_r($_FILES);
echo $name = $_FILES['file']['name'];
?>
<html>
<header>
<title> Test Page</title>
</header>
<body>
<form action="Test.php" method="POST" enctype="multipart/formdata">
<input type="file" name="file" id="file" /><br><br>
<input type="submit" value="submit" />
</form>
</body>
<html>
and the outputted echo is only Array ( ) whether I upload a file or not.
anyone faced something like this before ?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
session_start();
$file_tmp= $_FILES['image']['tmp_name'];
header("Test.php");
}
?>
<html>
<header>
<title> helllllllo </title>
</header>
<body>
<form action="form-validation" method="POST" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<div class="form-row form-input-name-row">
<label>
<span>Profile Image</span>
<input type="file" name="image" id="file" onchange="readURL(this);" data-val="true" style="margin-bottom:10px;" />
<div class="form-row form-input-name-row">
<img id="blah" src="#" alt="your image" style="width: 100px; display: none; margin-bottom:10px;" />
</div>
</div>
<input type="submit" value="submit" />
</form>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').show().attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help
I have a single profile page that I want to upload a photo on as a separate action. I have the first form submitting to the page successfully, it is when I submit the photo form that the page returns blank with an empty message.
HTML
<form method="post" action="profile.php" id="main">
<input name="txtFirstName" type="text" value="<?php echo $sFirstName; ?>">
<input type="submit" name="btnSubmit" value="Submit" />
</form>
<form method="post" action="profile.php" id="photo_upload" enctype="multipart/form-data">
<img src="<?php echo($sPath); ?>" height='100' width='100' id="imgProfile" />
<br>
<input type="file" name="fUpload" id="fUpload">
<br>
<input type="submit" name="btnUploadPhoto" value="Upload" class="cancel"/>
</form>
PHP
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_POST['btnSubmit']) {
$sFirstName = $_POST['txtFirstName'];
}
else if ($_POST['btnUploadPhoto']) {
// DO MY MOVE LOGIC
}
}
When the "btnSubmit" is called the page loads and the textbox gets the name that was entered. When the "btnUploadPhoto" is called I get this on the screen and nothing else:
{error: '', msg: '' }
How do I get the page to reload with the original form?
As you are uploading a file, you will want to check if a file has been submitted in your PHP code. Here is the code to do this:
isset($_FILES['fUpload'])