After creating a form and typing the php code, i have a form where i am able to input a file, specifically an image, and without reciving any error messages, as well as getting the success message in my url, the image uploaded still can not be found in its destination folder.
Here is the HTML
<form action="" class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Find Art Here">
</div>
<button type="submit" class="btn btn-primary">Search</button>
</form>
and here's the php
<?php
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileName = $file['name'];
$fileTmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileError = $file['error'];
$fileType = $file['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allow = array('jpg', 'jpeg', 'png', 'pdf');
if (in_array($fileActualExt, $allow)) {
if($fileError === 0){
if($fileSize < 5000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'uplaods/'.$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
header("Location: index.php?uploadsucess");
} else{
echo "Your file is too big";
}
} else {
echo "There was an error uplaoding your file!";
}
} else{
echo "You cannot upload files of this type!";
}
}
Code is based and modified from this video : https://www.youtube.com/watch?v=JaRq73y5MJk
Any help would be much appreciated!
in your form add this
enctype="multipart/form-data"
example
<form method="post" class="login-form" enctype="multipart/form-data"
action="my.php">
1) Add enctype="multipart/form-data" in your form tag.
2) fileDestination = 'uplaods/'.$fileNameNew;spelling mistake on file name. (If it is intentional, then my bad).
3) <input type='file'> type should be file.
how did you communicate with $_FILES? You need to use
type="file" in your input
The first thing you have to do is adding enctype="multipart/form-data" and method="post" to your form-tag:
<form action="" method="post" enctype="multipart/form-data">
If it then still doesn't work, check the upload_max_filesize and the post_max_size settings in your php.ini. If your file is too big, only move_uploaded_file() will return false, so check that function as well.
Your <form> misses an attribute to send files:
enctype="multipart/form-data"
And to be sure the upload is done, put the move_uploaded_file() in a condition because the function returns a boolean :
if (move_uploaded_file($fileTmpName, $fileDestination)) {
header("Location: index.php?uploadsucess");
} else {
header("Location: index.php?uploadfail");
}
Also, a better way to get your file extension is using pathinfo() :
pathinfo($fileName, PATHINFO_EXTENSION);
you don't have any <input type="file"> in your form
you maybe mispelled your upload directory : uplaods
php.net - move_uploaded_file()
php.net - pathinfo()
Related
I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.
Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.
This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.
I have a form that allows users to create events. Inside this form I have a form for an image upload. The HTML looks like this:
<form id="upload_event" method="post" action="system/upload_event.php">
<input type="text" name="title">
<textarea name="description"></textarea>
<form id="upload_image" method="post" action="system/upload_image.php">
<!-- code -->
</form>
Upload event
<input style="display:none" type="submit" id="submit_upload_event" name="submit_upload_event">
</form>
This is what my upload_image.php, which processes the image upload, looks like:
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
if(!in_array(strtolower($extension), $allowed)){
echo '{"status":"error"}';
exit;
}
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
}
echo '{"status":"error"}';
exit;
As I found out, it is not possible to nest a form into another one. What would be the solution here?
If you're using Ajax you could return a value from your upload form to your main form. Like putvande said you can't have a form inside a form.
I am writing an app from which user will upload files on the server. I have found a php script from internet but I don't know how am I going to tell the script where to upload the data. This might be a silly question but I am no PHP programmer. I am using this php script in my java code.
Here is the script.
<?php
$filename="abc.xyz";
$fileData=file_get_contents('php://input');
echo("Done uploading");
?>
Regards
This is a terrible way of uploading files, you are much better off using a form and the $_FILES superglobal.
Take a look at the W3Schools PHP File Upload Tutorial; please read all of it. For further reading take a look at the PHP Manual pages on file upload.
The file input type will create the upload box in the html form:
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
After error checking and validating that the file is what you are expecting (very important: allowing users to upload anything to your server is a huge security risk), you can move the uploaded file to your final destination on the server in PHP.
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/abc.xyz");
The filename is actualy the file path along with the name of the new file, set the path there and a file will be created with write permissions.
Make sure you give the servers full path not the relative one and that you have the required permission to create a file there.
Always refer to the PHP Manual
Here's a basic example to get you started:
HTML:
<html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
Choose a file to upload: <input name="uploaded_file" type="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
PHP:
<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'/upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
echo "It's done! The file has been saved as: ".$newname;
} else {
echo "Error: A problem occurred during file upload!";
}
} else {
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
} else {
echo "Error: No file uploaded";
}
?>
Refer to the documention for more information.
Hope this helps!
htm code :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Movie :<br />
<input name="fileField" type="file" size="30" /><br />
<input name="submit" type="submit" class="submit" value="Send" />
</form>
php code :
if ($_FILES['fileField']['tmp_name'] !=""){
$fileName = $_FILES["fileField"]["name"];
$fileType = $_FILES["fileField"]["type"];
$fileTmpLoc = $_FILES["fileField"]["tmp_name"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
move_uploaded_file($fileTmpLoc, '../upload/video.flv');
}
this code not work for VIDEO file but work correct for other file ( jpeg , mp3 , png and ,,, )
Perhaps change your directory to
move_uploaded_file($fileTmpLoc, '../upload/ ');
this problem from php setting
php set to Max 2MB size for upload
You have absolutely no error handling on the code, meaning you have no way of telling when an upload failed. Add:
if ($_FILES['fileField']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error " . $_FILES['fileField']['error']);
}
as some bare-minimum error handling. The error codes are defined here: http://php.net/manual/en/features.file-upload.errors.php. Checking for the absence of a 'tmp_name' is NOT a proper check.
I am building a php application.
I can easily upload an image or any other type of data, but not a .jar. Below my code:
Upload.php
<form action="getfile.php" method="post" name="uploadForm" id="uploadForm" enctype="multipart/form-data" ><br>
<?php echo gettext("Image "); ?>
<input name="imagen" value="" type="file" id="imagen" />
<?php echo gettext("Jar "); ?>
<input name="jarFile" value="" type="file" id="jarFile" />
</form>
getfile.php
$fileName = $_FILES['jar']['name'];
$fileType = $_FILES['jar']['type'];
//Check the extension
if (!strpos($fileType, "jar") ) {
echo gettext("The simulation must be jar extension. Try again.");
}else{
if (move_uploaded_file($_FILES['jar']['tmp_name'], $path)){
echo gettext("Simulation stored succesfully.");
}else{
echo gettext("Something happened. Try again. ");
}
}
For images I am following the same aproach, but when I try to upload a jar file, I am always getting the error
The simulation must be jar extension. Try again
and $fileType is empty. Is there some restriction at this point? Am I missing something??
Thanks in advance
The type property is unreliable (and populated by the browser). Your best bet is to retrieve the MIME file type:
$fhandle = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fhandle, $_FILES['jar']['tmp_name']);
The $mime_type should be application/java-archive.
PHP >= 5.3.0