I am trying to upload a quakeml file type format. It needs to be stored in a directory via a form but it does not upload anything. I have already written this code below.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<body>
<?php
if(isset($_FILES['file'])){
$file_name = $_FILES['file']['name'];
$file_size =$_FILES['file']['size'];
$file_tmp =$_FILES['file']['tmp_name'];
$file_type=$_FILES['file']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
$expensions= array("quakeml");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a quakeml";
}
if($file_size > 4097152) {
$errors[]='File size must be excately 4 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>
</html>
Why is the above not uploading anything?
Related
I was trying to upload a folder inside my htdocs folder in XAMPP.
I followed the rules of move_uploaded_file still did not work.
Here's my current code:
<?php
if(isset($_POST['submit'])){
$allowed_ext = array('png', 'jpg', 'jpeg', 'gif');
if(!empty($_FILES['upload']['name'])){
print_r($_FILES);
$file_name = $_FILES['upload']['name'];
$file_size = $_FILES['upload']['size'];
$file_tmp = $_FILES['upload']['tmp_name'];
$target_dir = "uploads/{$file_name}";
// Get file ext
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
// Validate file ext
if(in_array($file_ext, $allowed_ext)) {
// verify size
if($file_size <= 1000000) { // 1000000 bytes = 1MB
// Upload file
move_uploaded_file($file_tmp, $target_dir);
$message = '<p style="color: green;">File uploaded!</p>';
} else {
$message = '<p style="color: red;">File to large</p>';
}
} else {
$message = '<p style="color: red;">Invalid file type</p>';
}
} else {
$message = '<p style="color: red;">Please choose a file</p>';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<?php echo $message ?? null; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="upload" />
<input type="submit" value="submit" name="submit" />
</form>
</body>
</html>
Right now the images are not getting move to the uploads folder inside my current directory.
Any idea why?
Try this:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 1000000){
$errors[]='File size must be excately 1 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name); //create a folder 'uploads' in your folder
echo "Success";
}else{
print_r($errors);
}
}
?>
i just moved from asp to php and i'm able to upload files to the server correclty. Now what i want to do is to rename the file before the upload is done and also echo the newly named file
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../complains_photos/".$file_name);
echo $file_type;
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
Just change the name that you are passing to the move_uploaded_file function. Something like this:
$destinationFileName = date('Ymd').'.'.$file_ext;
move_uploaded_file($file_tmp,"../complains_photos/".$destinationFileName);
Also change your echo to send $destinationFileName
Change move_uploaded_file(...); statement in the following way,
move_uploaded_file($file_tmp,"../complains_photos/".date("d-m-Y").".".$file_ext);
You can change the date format as per your preference.
Reference: http://php.net/manual/en/function.date.php
Hello guys, please help!
I'm having problems with my project, I followed these steps on how to upload(https://www.youtube.com/watch?v=Ipa9xAs_nTg) but something is wrong.
The error is Notice:
Undefined index: tmp_name in C:\xampp\htdocs\LDEVERACATERING\upload_process.php on line 16
Here is my upload.html
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.2.1.min"></script>
<script src="jquery-migrate-1.4.1.min"></script>
<title>
Image Upload
</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="content">
<form method="POST" action="upload_process.php" enctype="multipart/form-data">
<input type="hidden" name="size" value="1000000"/>
<div>
<input type="file" name="image"/>
</div>
<div>
<textarea name="text" cols="40" rows="4" placeholder="Say something about this image..."></textarea>
</div>
<div>
<input type="submit" name="upload" value="Upload Image"/>
</div>
</form>
</div>
</body>
</html>
here is my upload_process.php
<?php
$msg = "";
//if upload button is pressed
if (isset($_POST['upload'])) {
//path to store the upload image
$target = "photos/".basename($_FILES['image']['name']);
//connect to database
$db = mysqli_connect("localhost", "root", "", "catering_info");
}
//get all the submitted date from the form
$image = $_FILES['image']['name'];
$text = $_POST['text'];
$sql = "INSERT INTO photos_upload(image, text) VALUES('$image', '$text')";
mysqli_query($db, $sql); //stores the submitted date into the database table: images
//now let's move the upload image into the folder: photos
if (move_uploaded_file($_FILES['tmp_name']['name'], $target)) {
$msg = "Image uploaded successfully";
}
else
{
$msg = "There was a problem uploading image";
}
?>
PLEASE HELP ME, I REALLY NEED IT. Also I'm still new to this, I just started web prog last 3 weeks :( Thank you very much!
Please try this
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$expensions= array("jpeg","jpg","png");
if(in_array($file_ext,$expensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
Try this by replacing your code
move_uploaded_file($_FILES['tmp_name']['name'], $target)
With these
move_uploaded_file($_FILES['image']['tmp_name'], $target)
I was trying to upload a picture file and I wanted to display the information of that picture. But my code is not working, I'm not sure what's the problem, I hope someone could point out what am I doing wrong.
<!DOCTYPE html>
<html>
<head>
<title>Upload Image</title>
</head>
<body>
<form action="upload_image.php" action="post" enctype="multipart/form-data">
Select image: <input type="file" name="image">
<input type="submit" name="upload" value="Upload Now">
</form>
<?php
if(isset($_POST['upload'])){
echo $image_name = $_FILES['image']['name'];
echo $image_type = $_FILES['image']['type'];
echo $image_size = $_FILES['image']['size'];
echo $image_tmp_name = $_FILES['image']['tmp_name'];
if($image_name == ""){
echo "<script>alert('Please select an image!')</script>";
exit();
}
}
?>
</body>
</html>
ps: I'm still new to php and file upload, so please go easy on me..Thank you in advance
The problem appears to be in the fact that you didn't specify any location to where to upload it, neither the file type. Here is the code with Extension verification, Size, and moving the file in a folder called images.
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions = array("jpeg","jpg","png");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file."
}
if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
I have wrote a simple PDF uploader in PHP. It works fine and the PDF uploads fine. However I would like to print the file name and the file path (where it is being saved). On the upload.php form. Is this possible?
upload.php
<form action="../pdf/upload_pdf2.php" method="POST" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="image"/>
<label><?php echo strlen($filename ) > 0 ? $filename : "" ?> will be saved to /public_html/dev/pdf</label>
<p> </p>
<input type="submit" class="upload-button"/>
</form>
upload_pdf2.php
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
$extensions = array("jpeg","jpg","png","pdf");
if(in_array($file_ext,$extensions )=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){
$errors[]='File size must be excatly 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"../pdf/".$file_name);
echo "File uploaded sucessfully";
}else{
print_r($errors);
}
}
?>