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);
Related
the form upload files can not be displayed, and this is my form upload code
<?php
$imageinfo = getimagesize($_FILES['img']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
echo "<center><br>Sorry, we only accept GIF and JPEG images</br><br>param name: img<br>u can upload
with CSRF";
exit;
}
$uploaddir = 'ex/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" size="20" name="img" />
<input type="submit" name="upload" value="Upload" />
</form>
the result is
Sorry, we only accept GIF and JPEG images
param name: img
u can upload with CSRF
i want to display form upload files
This will work you should specify correct path
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$imageinfo = getimagesize($_FILES['img']['tmp_name']);
if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') {
echo "<center><br>Sorry, we only accept GIF and JPEG images</b><br>param name: img<br>u can upload
with CSRF";
exit;
}
else{
$uploaddir = '../path-to-move/';
$uploadfile = $uploaddir . basename($_FILES['img']['name']);
if (move_uploaded_file($_FILES['img']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "File uploading failed.\n";
}
}
}?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" enctype="multipart/form-data">
<input type="file" size="20" name="img" />
<input type="submit" name="upload" value="Upload" />
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 have the following which uploads a single file and works fine:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile" type="file" />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
When i adpat this to accept multiple files for upload, it doesnt seem to work. I dont get any errors / warnings so i am completely stumped. Here is my multiple file upload code:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile[]" type="file" multiple />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["userfile"]["name"][$key];
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile);
{
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
Any suggestions on what could be wrong?
You use wrong key in $_FILES, you have to use $_FILES['userfile'], not $_FILES['files']:
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
<input name="userfile[]" type="file" multiple />
<input type="submit" value="Upload" />
</form>
<?php
$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["userfile"]["name"][$key];
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile);
{
echo "File has been successfully uploaded.\n";
} else {
echo "Upload failed";
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
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.