iis_iusrs has read and write in the uploads dir "c:\temp" and in the destination folder "C:\inetpub\wwwroot\test\uploads".
After my script runs a tmp file is created. in the c:\temp dir however the file is not moved. When it runs on chrome it says "Success".
<?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 > 112097152) {
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true) {
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action = "" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "image" />
<input type = "submit"/>
<ul>
<li>Sent file: <?php echo $_FILES['image']['name']; ?>
<li>File size: <?php echo $_FILES['image']['size']; ?>
<li>File type: <?php echo $_FILES['image']['type'] ?>
</ul>
</form>
</body>
</html>
Related
Problem:
I have created a upload.php file, which contains a HTML form and the PHP code needed to upload images and I use XAMPP localhost:
htdocs/web2/assets/upload.php
I want to upload images to the folder upload in the same directory:
htdocs/web2/assets/uploads/
Script:
<?php
<form action="?" method="post" enctype="multipart/form-data">
<!--wrap input button as around pre-existing image -->
<?php
echo '<label class="profile_gallery_image_in"><input type="file" name="fileToUpload" id="fileToUpload" onchange="form.submit()"/><p class="label"></p><img class="myImg" src='.$image.' height="100%" width="100%" /></label>';
?>
</form>
<!-- Commence Photo Upload -->
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
The form is set to auto-submit upon input type change. This appears to be working fine and the form is submitting.
However, I get no actual image uploaded and no error at all.
How do I solve this problem?
There seems to be an error in the code. You can use the below code for image uploading.
<?php
if(isset($_FILES['image'])){
$errors= array();
$dir = "images/";
$file_name = $_FILES['image']['name'];
$file_name = $dir. $file_name;
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
$file_type = $_FILES['image']['type'];
$tmp = explode('.',$_FILES['image']['name']);
$file_ext=strtolower(end($tmp));
$extensions= array("jpeg","jpg","png","gif");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a GIF, 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, $file_name);
echo "Success";
}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>
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
For my website, I have a PHP image file upload system so that users can upload their own pictures. Those pictures are stored in /images/, and I have changed its file permission to 777 . When I test the upload file, it works but the file only keeps the .jpg extension and not the prefix:
<?php
$name= 'filename';
$newname= '/images/'. $name. '.jpg';
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){
rename($file_tmp, $newname);
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>`
I haven't actually seen rename before, but just trying it, I'll admit its behavior doesn't seem to be quite what I expect. Try move_uploaded_file instead.
Most importantly, I notice you were checking that the only image formats you accepted were png and jpeg, yet you were renaming them all to jpg. Why? Not all image viewers will open a png with a jpg extension.
<?php
$name= 'filename';
$newname= '/images/'. $name;//. '.jpg'; // why wouldn't you preserve the correct extension? i.e. $file_ext
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){
// // // use move_uploaded_file() instead // // //
//rename($file_tmp, $newname);
move_uploaded_file($file_tmp, "$newname.$file_ext");
echo "Success";
}else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
I've created a form in html, for upload image, it seems work perfect because when I upload the image it give me the string "Success", but i can't find the file in the server.
this is my code:
<?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,"./photo/".$file_name);
echo "Success";
}
else{
print_r($errors);
}
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
<ul>
<li>Sent file: <?php echo $_FILES['image']['name']; ?>
<li>File size: <?php echo $_FILES['image']['size']; ?>
<li>File type: <?php echo $_FILES['image']['type'] ?>
</ul>
</form>
Check move_uploaded_file:
$ret_value = move_uploaded_file($file_tmp,"./photo/".$file_name);
if ($ret_value == false) die ("Ups! Couldn't actually move the temp file! No success at all.");
If it is returning false (error) the file could not be being actually moved from the temp file and you still would get the "Success" message, because no exception would arise.
If you're getting an error, maybe you can fix it by moving the file to the actual existing directory.
move_uploaded_file($file_tmp,"./poi/photo/".$file_name);
Your image upload is clearing every validation.
But, you are not storing it at correct paths:
Change:
move_uploaded_file($file_tmp,"./photo/".$file_name);
To:
move_uploaded_file($file_tmp,"./poi/photo/".$file_name);
Also, apply write permissions for the directory poi.
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);
}
}
?>