How to Upload a File within HTML Context - php

I'm trying to get a basic PHP script up and running where a user uploads a file to the server, and the file is saved to a folder on the server. I am using a HTML form that looks like
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50000000" />
<br />
<input type="submit" value="Upload" />
</form>
and I have a PHP file saved as upload_file.php that looks like
<?php
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) . "
has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The issue is that after the file is uploaded, it simply shows the php code. Nothing else occurs, and no image is saved in uploads. I've looked at several tutorials, and none of them seem to mention this. I'm guessing it's glaringly obvious, but I could use some help. The error echo "Sorry... never occurs either.
Thanks.
Quick Note: I'm using Apache to host a local web server.

If it simply shows the php code, then PHP is not running. Verify tha PHP is installed and mod_php5 activated.
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml

Related

Can't upload photo in database [duplicate]

I've attempted to write code to have a file uploaded to a "media" folder in PHP. For some reason it continues to not work.
Here's the execution code:
move_uploaded_file($_FILES["file"]["tmp_name"],"../media/" . $_FILES["file"]["name"]) or die ("Failure to upload content");
Here's my form code:
<input type="file" name="file" id="file" />
Any ideas why it may not be working?
EDIT:
When I use the command "print_r($_FILES);", it displays:
Array ( [file] => Array ( [name] => Screen Shot 2012-05-29 at 12.36.11 PM.png [type] => image/png [tmp_name] => /Applications/MAMP/tmp/php/phpHNj3nW [error] => 0 [size] => 71640 ) )
Image is NOT uploaded into the folder.
Make sure that in your form.. you put the enctype.
eg: <form method="post" enctype="multipart/form-data" action="index.php"></form>;
To check if files are successfully updated upon submitting the form. use print_r to see results. print_r($_FILES);
make sure media folder has 777 permission and the path ../media/ is correct
Put the encription in form
<form method="post" action="index.php" enctype="multipart/form-data"></form>
Check in php.ini file max_execution_time
heavy images are not uploaded due to execution time..
Check in php.ini file max_execution_time heavy images are not uploaded due to execution time..
eg: ;
There is a form with encrypt type or an ajax call? Do you check if the file is sended to the upload script (with a print_r($_FILES["file"]).
If correct, do you have check if the relative path is correct?
You must start from the current script (if file is included you must start from the including script).
Sorry if answer seems simply, but the posted code is a little too short to evaluate.
In your form tag you want something like this
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Make sure enctype is set to multipart/form-data for files. Replace uploader.php with the name of the php file doing the processing. Also make sure your permissions are set so the file can be created in the directory.
Here's a list of possible problems: http://php.net/manual/en/features.file-upload.php
Have you checked that the "web server user" has write permissions to "../media" ?
I was having the same problem. I am using ubuntu 18.04 and it was solved when i used this permission command on terminal.
sudo chmod -R 777 /var/www/html/target_dir.
->I have apache2 web server and target_dir as Download so replace target_dir as per your destination directory.
Make sure that in your form.. you put the enctype.
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success
If anyone wounder what's the simplest file php uploader this is the code I test it,
it will upload the file to current folder
IT'S NOT SECURE
This code for learning purpose only, don't upload it to working environment.
<html>
<body>
<form method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file1" id="file1" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
if ($_FILES["file1"]["error"] > 0) {
echo "Error: " . $_FILES["file1"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file1"]["name"] . "<br />";
echo "Type: " . $_FILES["file1"]["type"] . "<br />";
echo "Stored file:".$_FILES["file1"]["name"]."<br/>Size:".($_FILES["file1"]["size"]/1024)." kB<br/>";
move_uploaded_file($_FILES["file1"]["tmp_name"],dirname(__FILE__).'/'.$_FILES["file1"]["name"]);
}
}
exit ();
?>
credit https://stackoverflow.com/a/15709181/3019002
You need to set the folder permission to 777. otherwise your file won't load

Need assistance with a very basic php upload form [duplicate]

This question already has an answer here:
PHP: No input file specified
(1 answer)
Closed 5 years ago.
I realize that this is a very basic question. Well, to be honest, I am very new. This is the first time that I have worked with php.
The only thing that I am trying to do is to allow internal users to upload to a file to a temporary directory on the server. Here is the php and form code...
HTML
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
PHP
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
The "uploads" folder is located in the same directory as the html and php. I have verified permissions on that folder that allow the "users" group to read/write.
This is on IIS 8.5 and Windows Server 2012.
When I run this, choose a file and select "upload," it says "No file specified!"
Thanks in advance for any help and I look forward to any information that you can provide as this is my first post on here.
Try $target_path = $_SERVER["DOCUMENT_ROOT"]."/uploads";

Uploading files with PHP & HTML

I'm having trouble with setting up the file upload to my php script.
Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right.
EDIT: I have resolved my situation with the file not properly uploading. I have edited the blocks of code to what I have currently working. If needed, please refer to the pre-edited version to see changes :).
Cheers.
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;
if(isset($_FILES["uploadFile"])){
$tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){
echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
The $targetnameis set to whatever the user inputs in the field. In this case, I have it as their email.
$targetname = filter_input(INPUT_POST, 'email');
This is what the <html> for the file upload looks like:
<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
<p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
<input type="submit" value="Upload File">
It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here?
$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
This would be like:
C:/xampp/htdocs/uploaddir/me#gmail.com
This runs:
$tardir= $tardir . basename($_FILES["uploadFile"]["name"]);
then:
C:/xampp/htdocs/uploaddir/me#gmail.comfilename
shouldn't it be:
C:/xampp/htdocs/uploaddir/me#gmail.com/filename
you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. If I get a bug like this I dump everything and echo every string until I find it..

bool(false) Uploading a file

I have had problems with a simple php script in which I can upload a file to a certain folder. I have tried multiple ways in doing this and I still have not had success.
Any errors in my code or advice on how to correct the issue will be taken gracefully.
Main Php Code:
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
Choose Photo:
<input name="userfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
<?PHP
if ($userfile_size>250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($userfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
</form>
</label>
</form>
Php code that is called upon on click (upload_photos.php)
<?php
$target_path="uploads/";
chmod("uploads/", 0755);
$target_path=$target_path . basename( $_FILES['uploadedfile']['name']);
$test=move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
if($test) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
var_dump($test);
}
?>
I do not understand why my end results [upon clicking "Upload Files" Button] include only the following results:
"There was an error uploading the file, please try again!bool(false)"
One more thing: I have also tried using the full computer folder path for $target_path and chmod.
Does anybody see what I am doing wrong?
You have <input name="userfile" but then use $_FILES['uploadedfile'] in your script - use one or the other.
Other than that, make sure the chmod worked and the folder is writable.
bool(false) is the output of var_dump($test);, indicating that move_uploaded_file is returning false.
As a basic debugging step, you should try var_dump($_FILES) to make sure you're accessing the right element of that array (I can tell from your code that you aren't, the index will be the name attribute of your <input type="file"/> element).
You have at least one other serious flaw in your logic... The PHP code in your upload form doesn't make any sense. That block of PHP code will execute server-side before the user has ever uploaded a file. It can't possibly work. The two variables you're checking, $userfile_size and $userfile_type, are not defined anywhere.
In my case, I forgot to create a folder where I want to upload. So check once the specified upload path is available or not.

Calling jrxml file from php for pdf generation

i have to generate jasper reports in my php website.
I want to know what all are needed to make this work. I was told that i needed php-java bridge. But i hope that is for the sole purpose of generating a .jrxml file.
I already have the jrxml file with me.
Now how can i call this file from my php code, for generating jasper report in pdf format ?
In case this file is located on your server you can open it using file_open from any php script.
EDIT: The easiest way to do it seems to be using something like the php-jasper-integration. This way you don't need to use a java-php bridge.
In case you do not have it onyour server but client side only you will need to upload it to your web site and treat this file as you need to using your website script.
Here is some example code to do it:
if(!isset($_FILES['userfile']['tmp_name'])){
// starte Session
//session_start(); // Headers sent out
?>
<form enctype="multipart/form-data" action="myscript.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<!-- Name of input element determines name in $_FILES array -->
Please upload a file.
<br>
<br>
<input name="userfile" type="file" />
<input type="submit" value="UPLOAD" />
</form>
<?php
}
else {
?>
if ($_FILES['userfile']['tmp_name'] == '') die ('No file submitted!');
$target = "uploaded/";
$target = $target . basename( $_FILES['userfile']['name']) ;
$ok=1;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded. Parsing will start soon. ";
}
else {
echo "Sorry, there was a problem uploading your file.<br>";
}
// now do what you need with your file in $_FILES['userfile']['tmp_name']
Well, i could get what i wanted by using PHP Jasper XML. It is opensource too.
Well jrxml is created with ireports and its executed with java, you need a server like tomcat. look for information of jru-php it will help you

Categories