PHP file Uploads not Working - php

I'm working on a web application in php that allows users to upload files with a specified structure (file type could be cvs or excel sheets) and the app will extract data from the files and insert them into a database.
I can't get my app to upload files and I've been trying for 2 days, I checked the php.ini for max upload size ,max post size , file uploads on, temp directory set to /tmp which is accessible by all users.
I also checked the syntax of php and html and made sure I was using the right encoding type, I also checked the permissions of the upload directory but the _FILES variable is always empty, note : my web server is hosted on amazon ec2 running Ubuntu 14.04 LTS.
here's one of the codes I tried and it's output:
<?php
echo $_FILES['file']['error'];
print_r($_FILES);
echo $name = $_FILES['file']['name'];
?>
<html>
<header>
<title> Test Page</title>
</header>
<body>
<form action="Test.php" method="POST" enctype="multipart/formdata">
<input type="file" name="file" id="file" /><br><br>
<input type="submit" value="submit" />
</form>
</body>
<html>
and the outputted echo is only Array ( ) whether I upload a file or not.
anyone faced something like this before ?

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
session_start();
$file_tmp= $_FILES['image']['tmp_name'];
header("Test.php");
}
?>
<html>
<header>
<title> helllllllo </title>
</header>
<body>
<form action="form-validation" method="POST" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<div class="form-row form-input-name-row">
<label>
<span>Profile Image</span>
<input type="file" name="image" id="file" onchange="readURL(this);" data-val="true" style="margin-bottom:10px;" />
<div class="form-row form-input-name-row">
<img id="blah" src="#" alt="your image" style="width: 100px; display: none; margin-bottom:10px;" />
</div>
</div>
<input type="submit" value="submit" />
</form>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').show().attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>

Related

Can't display image I uploaded in PHP

I'm trying to upload an image and display after uploading, the upload part works fine but image can't display.
Any answers?
Code:
<!DOCTYPE html>
<html>
<body>
<?php
echo <<<_END
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
_END;
if($_FILES){
$name = $_FILES['fupload']['name'];
move_uploaded_file($name = $_FILES['fupload']['tmp_name'], $name);
echo "<br><img src='$name'>";
}
?>
</body>
</html>
Browser:
Image can't display
nevermind, the problem is move_upload ($name=xxxx, $name), it means you assign to $name the tmp source !
here is a working code
<!DOCTYPE html>
<html>
<body>
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="fupload" size="100000" accept="image/*">
<input type="submit" name="upload" value="Upload">
</form>
<?php if($_FILES)
{
$source=$_FILES['fupload']['tmp_name'];
$target1 = $_FILES['fupload']['name'];
move_uploaded_file($source,$target1);
?>
<br>
source=<?php echo htmlspecialchars($source);?>
<br>
target=<?php echo htmlspecialchars($target1);?>
<img src="<?php echo htmlspecialchars($target1);?>"
<?php
} // if $_FILES
?>
</body>
</html>
Ok, following comment, it seems $name point To à path not accessible for external user. Try a link like this $name="c:\path To your base path\www\et.png"
Edit: supposing you have a existing www folder , where you find your index.php. It may be called public.

Trouble accessing HTML form data using PHP

I'm trying to build a file upload form and I'm having trouble with the very basics. My form is this:
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit"/>
</form>
</body>
</html>
My php code so far is one line and it doesn't do anything:
<?php
echo $_POST['filename'];
?>
The idea (at this point) is just to display the name of the file entered in the form. What am I doing wrong?
Based on your code I modified it. Have a try it.
HTML Part
<html>
<body>
<form action="fileuploader.php" method="POST" enctype="multipart/form-data">
<input type="file" name="filename" />
<input type="submit" name="submit" />
</form>
</body>
</html>
PHP
if (isset($_POST['submit'])) {
// Check if files array is not empty
if (!empty($_FILES)) {
$imageName = $_FILES['filename']['name'];
echo $imageName;
// Insert your code related to upload
}
}
You can print the filename using the following code:
<?php
echo $_FILES["filename"]["name"];
?>

Input type text is not receiving in php file on submit

I want to send title of the image on submit of form but its not receiving in upload.php i don't know the reason only undefined variable warning is displayed. help anybody.
multiupload.php
<!DOCTYPE html>
<html>
<head>
<title>Upload Multiple Images Using jquery and PHP</title>
<!-------Including jQuery from google------>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="script.js"></script>
<!-------Including CSS File------>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<div id="maindiv">
<div id="formdiv">
<h2>Multiple Image Upload Form</h2>
<form enctype="multipart/form-data" action="" method="post">
First Field is Compulsory. Only JPEG,PNG,JPG Type Image Uploaded. Image Size Should Be Less Than 100KB.
<hr/>
<input type="text" name="title" />
<div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>
<input type="button" id="add_more" class="upload" value="Add More Files"/>
<input type="submit" value="Upload File" name="submit" id="upload" class="upload"/>
</form>
<br/>
<br/>
<!-------Including PHP Script here------>
<?php include "upload.php"; ?>
</div>
<!-- Right side div -->
<div id="formget"><a href=http://www.formget.com/app><img src="formget.jpg" alt="Online Form Builder"/></a>
</div>
</div>
</body>
</html>
upload.php
<?php
if (isset($_POST['submit'])) {
echo 'hell';
$j = 0; //Variable for indexing uploaded image
$value = isset($_POST['title']) ? $_POST['title'] : '';
echo 'title:'.$value;
$target_path = "uploads/"; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array
$ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
if( move_uploaded_file($_FILES["file"]["tmp_name"][$i], $target_path . $_FILES["file"]["name"][$i])){
echo ($i+1).')'. $_FILES["file"]["name"][$i]. '.<span id="noerror">is uploaded successfully!.</span><br/><br/>';
} else {//if file was not moved.
echo $i. ').<span id="error">please try again!.</span><br/><br/>';
}
}
}
?>
error/warning:
( ! ) Notice: Undefined index: title in C:\wamp\www\multiple_image_upload\upload.php on line 5
Two reason for this
1)you forget input type for title in your form
<input type="text" name="title" />
2)May be instead of $_POST['title'] it is
$_POST['tender']
in your upload.php

Why image didn't appear and display in another page of php after submitted?

I going to design 2 php page,one is personal detail form must filled by the users and another one is display all personal details where they had been done in personal detail form after submit.My problem is that the image that had submitted didn't display on the 2nd page.What wrong with my code?My code shown as below:
<!DOCTYPE html>
<head>
<title></title>
//for preview a image
<script type="text/javascript">
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
</script>
</head>
<body>
<form name="rform" method="get" action="researchers.php">
<h4>Researchers Profile</h4>
<fieldset>
<legend>Personal Details</legend>
<form id="form1" method="get" action="researchers.php" enctype="multipart/form-data" id="pro_image">
<input type='file' onchange="readURL(this);" name="image" id="image"/>
<br><img id="blah" src="profile pic.jpg" alt="your image" width="160px" height="120px"/><br/>
</form>
<input type="submit" name="savebtn" value="Save"/>
</form>
The researchers.php page
<?php
$name=$_FILES['image']['name'];
$tmp=$_FILES['image']['tmp_name'];
$new=time().$name;
$new="upload/".$new;
move_uploaded_file($tmp,$new);
if($_FILES['image']['error']==0)
{
?>
<br /><img src="<?php echo $new;?>" width="100" height="100"/>
<?php
}
?>
Try changing your <form> method to post as from the php manual for $_FILES - http://www.php.net/manual/en/reserved.variables.files.php
Description
An associative array of items uploaded to the current script via the HTTP POST method.
You will need to also add enctype="multipart/form-data" - http://www.w3.org/TR/html401/interact/forms.html#adef-enctype
<form name="rform" method="post" action="researchers.php" enctype="multipart/form-data">
see also http://www.php.net/manual/en/features.file-upload.post-method.php

PECL Uploadprogress Will Not Give Me Upload Progress

I am trying to implement a very basic AJAX upload progress bar using the PECL uploadprogress extension. I have found this sample code which works across all browsers: http://svn.php.net/viewvc/pecl/uploadprogress/trunk/examples/. It uses iframes to write the updates to. I would like to get the updates and do some jquery to build a progress bar. Here is my code (I know I did not write in code to account for when the upload ends) client.php:
<?php
$id = md5(microtime() . rand());
?>
<!DOCTYPE html>
<html>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function getProgress(){
$.get("progress.php", {"ID":'<?php echo $id ?>'}, function(data){
console.log(data);
});
window.setTimeout(getProgress(), 5000);
}
</script>
<body>
<form onsubmit="getProgress()" target="_self" enctype="multipart/form-data" method="post">
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
<label>Select File:</label>
<input type="file" name="file" />
<br/>
<label>Select File:</label>
<input type="file" name="file2" />
<br/>
<label>Upload File:</label>
<input id="submitButton" type="submit" value="Upload File" />
</form>
</body>
</html>
And progress.php:
<?php
if (function_exists("uploadprogress_get_info")) {
$info = uploadprogress_get_info($_GET['ID']);
} else {
$info = false;
}
$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;
echo $progress;
I error out and all that prints is 0's. Any ideas?
Try replacing
$progress = ($info['bytes_uploaded']/$info['bytes_total'])*100;
with
$progress = ($info['bytes_uploaded']*100)/$info['bytes_total'];
Both $info['bytes_uploaded'] and $info['bytes_total'] are integers, so division is not a float but is rounded down to a integer.

Categories