The code
<?php
$allowed_filetypes = array('.jpg','.gif','.bmp','.png');
$max_filesize = 5242888;
$upload_path = '/files';
$filename =$_FILES['userfile']['name'];
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1); //Get the extension form the filename.
if(!in_array($ext,$allowed_filetypes))
die('the file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['size'])>$max_filesize)
die('the file you attempted to upload is too large.');
if(!is_writable($upload_path)) {
die('you cannot upload to the specified directory,please CHMOD it to 777.');
}
if (move_uploaded_file( $_FILES['userfile']['tmp_name'],$upload_path.$filename))
{
echo 'you file upload successful.view the file here';
}
else{
echo 'failed';
}
When I upload a JPEG image, it shows the error "The file you attempted to upload is not allowed.". What's wrong with my code?
The main HTML code:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<button>upload</button>
Please use in form tag
<form action="upload.php" method="post" enctype="multipart/form-data">
Try replace:
$ext = substr($filename,strpos($filename,'.'),strlen($filename)-1);
//get the extension form the filename
by
$ext = substr($filename,strpos($filename,'.'),4);
//get the extension form the filename
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="file"/>
<input type="submit" name="sendFile" value="Upload" />
</form>
that should work.
Try with a submit button, <input type="submit " name="upload" value="upload"/>.
Related
In my index.php I have a form where a file upload will happen
<form id="uploadImage" method="post" action="upload.php">
<span class="input-group-text">
<label for="attach-doc" class="attachment-icon mb-0">
<i data-feather="image" class="cursor-pointer lighten-2 text-secondary"></i>
<input type="file" name="uploadFile" id="uploadFile" accept=".jpg, .png" /> </label></span>
</form>
I have a jquery for ajax submit as
$('#uploadFile').on('change', function(){
$('#uploadImage').ajaxSubmit({
// target: "#editor1 .ql-editor",
// resetForm: true
});
});
And my upload.php code is
<?php
//upload.php
if(!empty($_FILES))
{
if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
{
$ext = pathinfo($_FILES['uploadFile']['name'], PATHINFO_EXTENSION);
$allow_ext = array('jpg', 'png');
if(in_array($ext, $allow_ext))
{
$_source_path = $_FILES['uploadFile']['tmp_name'];
$target_path = 'upload/' . $_FILES['uploadFile']['name'];
if(move_uploaded_file($_source_path, $target_path))
{
echo '<p><img src="'.$target_path.'" class="img-thumbnail" width="200" height="160" /></p><br />';
}
//echo $ext;
}
}
}
?>
File locations are :
So as per the code when I click on the upload file a ajax call should happen and the image should be uploaded to the folder upload .
But upload is not happening with the above code, Any help appreciated.
You have missed one form attribute enctype="multipart/form-data" that is used for file uploading.
If the form tag does not have this attribute, then your file will not be uploaded to the server.
Please change following line:
<form id="uploadImage" method="post" action="upload.php">
to
<form id="uploadImage" method="post" action="upload.php" enctype="multipart/form-data">
I hope this will work for you
Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}
I know there's questions on this issue but I'm trying to get images uploaded to a directory using move_uploaded_file. I'm using the example from the php manual http://www.php.net/manual/en/function.move-uploaded-file.php but I'm getting an Undefined index: file. File is the name of my file input type so I'm not sure why it's undefined.
Here is my form:
<form action="MoveImages.php" class="dropzone" id="my-awesome-dropzone" method="post">
<input type="file" name="file" />
<input TYPE="submit" name="upload" title="Add Images" value="Add Photo"/>
</form>
Here's my PHP file:
<?php
$uploads_dir = '/Users/Jane/Desktop/NE';
$tmp_name = $_FILES["file"]["tmp_name"];
$name = $_FILES["file"]["name"];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
?>
Can anyone see where I am going wrong?
Add enctype="multipart/form-data" in your form tag.
That attribute is required when you are uploading file.
Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?
I have an HTML web form where users can upload multiple files. I am having trouble with moving the files though.
HTML:
My HTML:
<form enctype="multipart/form-data" method="post" action="save.php">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<input type="file" name="uploads[]" multiple="multiple" />
<input type="submit" name="submit" value="submit"/>
</form>
Save.php:
<?php
foreach ($_FILES['uploads']['name'] as $file) {
$target= UPLOADPATH . $file;
move_uploaded_file($file, $target)
or die('error with moving the file');
$file= time() . $_FILES['uploads']['name'];
echo $file;
}
What could I be doing wrong?
You have to use $_FILES['uploads']['tmp_name'] instead of $_FILES['uploads']['name'] for moving the file. name is the original file name but tmp_name is the current uploaded file path.