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
Related
I can't Upload the file using php because i can't send the path i got from the html file to the function FTP_PUT because it only takes string "test.txt"
How Can i send the Path to this function
PHP FILE
$file = $_POST["file"];
// upload file
if (ftp_put($ftp_conn, $file, $file, FTP_BINARY))
{
echo "Successfully uploaded $file.";
}
else
{
echo "Error uploading $file.";
}
// close connection
ftp_close($ftp_conn);
HTML FILE
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="well" action="Upload.php" method="post" >
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" name="file" id="file">
<!-- <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p> -->
</div>
<input type="submit" class="btn btn-lg btn-primary" value="Upload">
</form>
</div>
</div>
</div>
Use $_FILES["file"]["tmp_name"] instead of $_POST["file"]
edit:
$file = $_FILES["file"]["tmp_name"];
$file_name = $_FILES["file"]["name"];
// upload file
if (ftp_put($ftp_conn, $file_name, $file, FTP_BINARY))
or move the uploaded file first:
$target_path = "uploads/".basename($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"], $target_path);
Change your form tag to :
<form class="well" action="Upload.php" method="post" enctype="multipart/form-data">
If you don't include
enctype="multipart/form-data"
Nothing will get uploaded!
Check that the path is correct... I don't know your file structure so I'm guessing you need the full path, try...
if (ftp_put($ftp_conn, getcwd().$file, $file, FTP_BINARY)
I'm trying to save a file/image as session and then display it..
here's the first page which contains the form:
<form method="post">
<input type="file" name="picture" value="upload" id="file" accept="image/*" onsubmit="return validateForm()">
</form>
Ok, so on then next I save the image as session and try to show it (which is unsuccessful)...
Code:
<?php
if (isset( $_POST['picture']))
$_SESSION['picture'] = $_POST['picture'];
echo "<img src=" $_SESSION['picture'] " border='0' /> "
?>
I do not know what you will actually do but this is the approach that best fits
when you put a file type in your form, you need to use the global variable $ _FILES
form.html
<form action="process.php" method="post" enctype="multipart/form-data">
<label for="picture">Picture:</label>
<input type="file" name="picture" id="picture"><br>
<input type="submit" name="submit" value="Upload">
</form>
process.php
<?php
session_start();
//make sure you have created the **upload** directory
$filename = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"];
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
header('Location: display_picture.php');
display_picture.php
<?php
session_start();
?>
<div>
<img src="<?php echo $_SESSION['picture']; ?>" alt="picture"/>
</div>
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.
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"/>.