This is the code I'm using for my website. You can see the HTML input:
<form action="upload_request.php" method="post">
<input type="file" name="userfile" id="file"/>
<i>Only Excel files (*.xls || *.xlsx)</i>
</form>
And here there's the PHP script:
<?php
$uploaddir = '/cdir/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "uploaded.\n";
} else {
echo "fail!!\n";
}
?>
I have to upload an excel file in my folder dir but this script is not doing that and he always "fail!!". Could you help me please?
Your <form> tag should have enctype="multipart/form-data" attribute. See the example #1 here.
Related
So I want to allow users to upload an image and want to store that image in my server. (Using Ubuntu and Apache) so server path is (/var/www/html/)
My HTML code is:
<!DOCTYPE html>
<html>
<body>
<form enctype="multipart/form-data" action="uploadimages.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
</html>
and my uploadimages.php file code is:
<?php
$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
When I click the bottom send I get the following message:
File is valid, and was successfully uploaded.
Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => thisisthepictureIupload.png
[type] => image/png
[tmp_name] => /tmp/phpIUEUjE
[error] => 0
[size] => 4596
)
)
But when I go to the folder: /var/www/html/images the image is not there.
Any help would be highly appreciated.
I think you might be missing the / in your file path:
$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
should probably be
$uploaddir = '/var/www/html/images';
$uploadfile = $uploaddir . '/' . basename($_FILES['userfile']['name']);
I've trying to do an little script with which i can upload mp4-files to an webserver.
The problem is that it sometimes will upload the files and sometimes it wont.
<form enctype="multipart/form-data" action="index.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Diese Datei hochladen: <input name="userfile" type="file" />
<input type="submit" value="Send File" name="uploadfile" />
</form>
<?php
if ( isset( $_POST['uploadfile'] ) ) {
error_reporting(-1);
$uploaddir = '/var/www/vhosts/o190.orange.fastwebserver.de/httpdocs/perl/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
$uploadfile = preg_replace('/\s+/', '_', $uploadfile);
print $uploadfile;
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File sucessfully uploaded.\n";
} else {
echo "File couldnt be uploaded!\n";
}
print "</pre>";
}
?>
I've already changed "upload_max_filesize" and "post_max_size" to 1000M.
I've been trying to get my PHP upload script working, the HTML side seems to work but the PHP keeps returning a failed result. I am using iPage hosting. Here is my script:
<?php
if(isset($_FILES['userfile'])){
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
} else {
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
}
?>
Comment to answer to close the question, since that's what the issue was, both.
Check to see if the folder is writeable. If it is, then try a relative path instead of what you're using now.
I.e.: $uploaddir = 'uploads/';
Make sure the upload directory has the correct permissions - Writable and Proper Owner
You will run into issues when the file size is over 512000, you should check the size of the file using the $_FILE array and return an error message. Just a suggestion as you have already closed this.
// Error Checking Extended
if($_FILES['userfile']['error'] == 2) {
echo "You've exceeded the maximum file upload size of 512kb.";
return false;
}
I would really like your help.
I would consider myself an intermediate PHP programmer, but I have never used file uploads before.
I have been stuck on this problem for a long time.
This is a simplified version of my code and I'm 99% sure the error lies somewhere in here.
The output is always "The file wasn't an image file."
This is my HTML...
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" id ="partyPic"><br/>
<button type="button" onClick="uploadFile()">upload</button>
</form>
This is my PHP...
$image = $_FILES['image']['tmp_name'];
if (!isset($image)){
//Create default image.
}else{
$image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
$name = mysql_real_escape_string($_FILES['image']['name']);
$image_size = getimagesize($_FILES['image']['tmp_name']);
}
if($image_size == FALSE){
echo 'The file wasn\'t an image file.';
}else{
//I have code that successfully uploads stuff to my database.
}
If you could help it would be greatly appreciated.
Thank you,
Rick Ryan
Example Upload from http://www.php.net/manual/en/features.file-upload.post-method.php:
The basic Form:
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
The PHP:
<?php
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
The example from the manual.
So you should build apon something like this:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_FILES['userfile']['error'] == 'UPLOAD_ERR_OK'){
$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
list($width, $height, $type, $attr) = getimagesize($_FILES['userfile']['tmp_name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File was successfully uploaded.\n";
... Do Database stuff
}
}
?>
Your file input id is partyPic. You should use $_FILES['partyPic'].
Try this:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name ="image" id="partyPic"><br/>
<button type="button" onClick="uploadFile()">upload</button>
</form>
Use this after uploading your file.
move_uploaded_file($_FILES['image']['tmp_name'], $target);
Html sample code:
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Company Name:<input type="text" name="company"/><br />
ClientName: <input type="text" name="ClientName"/><br />
<br />
<input type="hidden" name="MAX_FILE_SIZE" value="5120000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
</body>
Sample Php code:
<?php
$file_name = $_POST['company'];
$random_digit = rand(0000,9999);
$new_file_name = $random_digit.$file_name;
$uploaddir = 'c:/temp/'.$new_file_name;
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
echo '['.$new_file_name.']';
print "</pre>";
?>
I am having problem with the rename. It renames the file but it keeps the original name too. So if I upload a file name joey.doc, it changes to random# = 4567, company name = whatever, file name = joey.doc output = 4567whateverjoey.doc. What I am looking for is for the file name to change to the random number append the company only = 4567whatever.doc. Later I will write code to create company name directory store all file from that company in their directory. Store the location tag in xml then pull up a table with to reference. the information. Plus I use the client name input box to decode what file go with what.
If writing xml is like asp.net C# I should have no problem
You are appending the old name again:
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
If you only want to append the extension have a look at pathinfo()
In the line:
$uploaddir = 'c:/temp/'.$new_file_name;
You add the new file name to the directory it's to be located in.
But in this line:
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
You also append the old name it was uploaded with to that string. You should do what #TimWolla suggested and find out about pathinfo() if you want to find just the extension.
change this line:
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
in particular just do some string manipulation on the basename(...) portion.
Basically, all you care about is the extension...and depending on your filename restrictions and desired robustness the answer could be easy, or a little bit tricky.
one example (so you get the idea):
$uploadfile = $uploaddir . '.' . split('.',basename($_FILES['userfile']['name']))[1];
or use built-in function Pathinfo