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

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";

Related

Multiple file uploads, how to include more fields and add full path to database

I found some code that uploads a file to a folder on my server and then saves the path to a database table column. the code for the form:
<form method="post" action="add_member.php" enctype="multipart/form-
data">
<p>Please Upload a Photo of the Member in gif or jpeg format. The file
name should be named after the Members name. If the same file name is
uploaded twice it will be overwritten! Maxium size of File is 35kb.
</p>
<p>
Photo:
</p>
<input type="hidden" name="size" value="350000">
<input type="file" name="cert_1">
<br/>
<br/>
<input TYPE="submit" name="upload" title="Add data to the
Database" value="Add Member"/>
</form>
And then the script with the algo that moves the file to the upload folder and then adds the full path to the database column
//This is the directory where images will be saved
$target = "upload";
$target = $target . basename( $_FILES['cert_1']['name']);
//This gets all the other information from the form
$pic=($_FILES['cert_1']['name']);
// Connects to your Database
mysql_connect("host", "db_user", "_db_pass") or
die(mysql_error()) ;
mysql_select_db("your_db") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO student_biodata_master
(cert_1)
VALUES ('$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['cert_1']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['cert_1']['name']). " has been
uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
Now, I need to increase the number of file upload fields to four at the least and then write the full path of the files to the database columns respectively. The script works very well but only does not write the full path into the database so i can call it later and display in my application. Can "anygoodbody" help with this? its actually doing my head in.
Thanks in advance for the great advice always.
Stack Overflow rocks!
You need html5 supported browser for multiple upload or use different technologies: java applet, flash, silverlight.
jquery has a plugin for multiple upload: http://blueimp.github.io/jQuery-File-Upload/

How to Upload a File within HTML Context

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

Uploading images with size limitations in PHP/MySQL

I'm trying to figure out how to upload imaged to a folder. I don't want to list it by listing all files in the directory.
What I want to do is have the user insert certain values to put into the MySQL, then take the file name when the file is finished uploading and insert it into a MySQL field "filename".
Thing is, I don't really know how to do file uploads in PHP, much less how to have it not add the information to the database until the file is fully uploaded. Could someone please help me out?
I also wanted to know how I could limit the max filesize to a certain size. I haven't been following technology for a while, so I wanted to know what the average size of an image would be of a photo take with an average digital camera now of days?
Lets make sure we have a form in a page. Lets call it form.php:
<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>
Then we create a file called uploader.php in the same folder as form.php.
// change the username and password to your database information
mysql_connect("localhost", "username", "password");
$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!";
}
// Here we will insert into the database
mysql_query("INSERT INTO fileuploads (filename) VALUES ('".$target_path."')");
Now make sure that you have a table called fileuploads with the field "filename".
Don't forget to create a directory in the same folder as form.php and uploader.php called "uploads"
I hope this will work for you.
Well you seem to have broken it down into the steps yourself so you seem to understand what to do.
The first thing you need is to upload the file in PHP. You can find a tutorial on it here. You will need a form in HTML and a PHP file which will handle it. This is also where you specify the file size.
Sample 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>
Sample 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!";
}
Then once you have uploaded the file you will need to store it in your database. You need to connect to your database (tutorial here)
Then you will need to add the information to the table, the filename is in the var $_FILES['uploadedfile']['name'] so you would need some code along the lines of
mysql_query("INSERT INTO files (Date, Filename)
VALUES ('$date', '$_FILES['uploadedfile']['name']')");
More information on inserting into a database can be found here
You can easily find out the PHP file upload details with example from this url http://php.net/manual/en/function.move-uploaded-file.php
And to verify the size, you should use getimagesize() function in php. Check the example code here http://php.net/manual/en/function.getimagesize.php

Simple function that stores images through a specific folder of the web application and retrieve it?

I am using WAMP and i have a table in my database that needs id,name,pathphoto,and etc.
I have a folder in my web application that stores all the photos of the users. And every user can upload a single photo of them and stores it in a specific folder, when users logged in, the photo that match the user will display in user's main page I am using html for my front-end and PHP for my back-end. And if the user decides to change the photo automatically the new photo will overwrite or removed the old photo of the user. What functions to use in this? I need all your suggestions or advice. Thank you.
Here it is how you do it, A rough code. Your html form
<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>
and your php page uploader.php
<?php
// Where the file is going to be placed
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
//a destination path with filename. Make sure your uploads folder have read write permission
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!";
}
//Save user info into database along with a filename that has been uploaded, i.e. basename( $_FILES['uploadedfile']['name']
Now you have uploaded file to a folder, when user edits his profile you can delete image using php's unlink function and upload a new image as you have done above
?>

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.

Categories