I have a webpage. There is an option to select file from the computer and then there is a submit button.
File will be same on each upload. I just need to select the file from my computer (location will be the same everytime) and then click on submit button.
Is there any way to do this automatically?
Yes, if that's the case, the HTTP POST submission will be the same each time. You can use any language of choice to send the POST submission over and over. There's also GUI tools to do this, namely Fiddler, you could watch the post submission, and then select it, and hold SHIFT + R to keep reissuing the request.
No, unless you don't have your page in your own computer. PHP cannot access user's local files, and not even javascript. All because of security. You have always select the file by your own hands, any code could not access your local files. But if the file is same every time, you can save it to your web server and use it.
Edit
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Insert file</label>
<input type="file" name="file" id="file"><br />
<input type="submit" name="submit" value="Submit">
</form>
PHP(upload.php):
<?php
$path = "upload/"; // uploads folder
$path = $path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path)) {
echo "File ". basename( $_FILES['uploadedfile']['name']). " uploaded.";
} else{
echo "Error, please try again.";
}
?>
Related
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
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
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
?>
I have an assignment for school, and I'm not sure how the teacher wants us to accomplish a task.
We need to get an uploaded file as a temp file only (index.php)
Output size of file (upload.php)
User can confirm save of file or not (upload.php)
So, I have the majority down, but my problem lies with creating the temp file into a permanent file.
index.php
<html>
<form action="http://mysite.org/~me/upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" value="Now upload it!">
</form>
</html>
upload.php
<?php
if (($_FILES["file"]["type"] == "application/vnd.ms-excel"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
}
?>
<form action="" method="POST">
<input type="submit" value"YES please save">
<form>
<?php
if (isset($_POST['submit']))
{
//Code for saving file
echo 'File saved!';
}
?>
Is it possible to go about it this way? My last echo statement does not work, so I'm doubtful the file save would be as well.
Hopefully the following comments can help you with the part you are stuck on.
In case you hadn't realized it already, any files uploaded with PHP are deleted once the PHP request that handled the uploaded file terminates. This means, if you don't do anything with the temp file from the upload, it will be deleted when the PHP script terminates.
One function of interest to you will be move_uploaded_file() which will move the temporary file from the upload to a permanent location of your choice.
Since the file will be uploaded and then you have to display the size and ask the user to confirm the upload, you will have to move the temp file to a permanent temporary location where it is kept when the user hasn't confirmed they want to keep the upload.
I'm not sure if you have been introduced to sessions yet, but if not, you will probably need some hidden form element that will keep track of what file they uploaded, otherwise you can keep this info in the session.
Then when the person submits the form saying they want to keep the file, you can move it again to a permanent location, or if they say no, then delete the file. The problem is, if they never say yes or no, then the file remains on the system.
Hope that helps.
Yep, this should work. Your if statements will catch the form submission and then echo your string there. A few little errors in your markup:
<input type="submit" value"YES please save">
Should be
<input type="submit" value="YES please save" name="submit">
Your final if statement in PHP is looking for a post variable named 'submit' but your <input type="submit"> tag has no name.
The file is saved to a temporary location when the upload completes. You can access this temporary file with $_FILES['file]['tmp_name'] BUT the file will be removed at the end of the request if you do nothing about it. This means that when the user clicks YES please save button, the file will not be available any more.
This means that you have to save the file to a disk in the first place, when you first call the upload.php file. There is no way to keep the file "in memory" while the user decides whether or not to save the file permanently.
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.