file not uploading on server php - php

Here is my code:
<form class="my_form" id="my_form" method="post" action="new_client.php" target="votar" enctype="multipart/form-data">
<div class="form-group">
<label for="file"> </label>
<div class="input-label-wrap">
<input type="file" class="" value="Добавить резюме" id="fileToUpload" name="fileToUpload" form="my_form">
</div>
</div>
<div class="btn_send">
<input type="submit" value="<?php echo $array1[12]?>" class="btn btn-danger btn-lg" name="send" id="send">
</div>
</form>
new_client.php:
$rand_pref = rand(10000, 99999);
$date = getdate();
$date = $date['year'].$date['mon'].$date['mday'].$date['hours'].$date['minutes'].$date['seconds'];
$target_dir = "admin/uploads/";
$target_file = $target_dir . $rand_pref . "_" . $date . "_" . basename($_FILES["fileToUpload"]["name"]);
$file_name = $_FILES["fileToUpload"]["name"];
$uploadOk = 1;
$fileType = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
//echo "Sorry, there was an error uploading your file.";
}
Im trying to send file to server but something wrong. I cant understand why it not working. Im using linux.
UPDATE:
just changed
uploads/
to admin/uploads/

Related

$_FILES[“image”][“tmp_name”] is empy with no data

I have written the following code. But I am getting empty $_FILES[“image”][“tmp_name”] with no value.
Php:
if ($_FILES) {
$imageFileType = strtolower(pathinfo("img/" . basename($_FILES["Image"]["name"]), PATHINFO_EXTENSION));
$target_file = "img/" . uniqid(rand()) . ".$imageFileType";
if (move_uploaded_file($_FILES["Image"]["tmp_name"], $target_file)) {
$pic = $target_file;
} else {
echo "Invalid File";
}
}
Html:
<form action="product-add.php" method="post" enctype="multipart/form-data">
<div class="products-form">
<p> Image:</p> <input type="file" name="Image" >
<input type="submit" value="ADD PRODUCT" class="btn">
</div>
</form>
Well it appears to me this is not right:
$target_file = "img/" . uniqid(rand()) . ".$imageFileType";
it should be
$target_file = "img/" . uniqid(rand()) . "." .$imageFileType;

Why file isnt uploading in the directory?

I am trying to upload files to the directory but I keep getting error or console
GET http://localhost:9999/store/uploads/.jgp
Whilst my html is
<form action="" method="POST" id="formSettingStoreLogo" enctype="multipart/form-data">
<div>
<p>Put a logo </p>
<br />
<input type="file" name="logo" />
<input type="submit" name="submitLogo" id="submitLogo" value="Upload" />
</div>
</form>
And code of php is
$target_dir = "../uploads/store/logo/";
$target_file = $target_dir . basename($_FILES["logo"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["logo"]["name"], $target_file)) {
echo "\n"."The file ". basename( $_FILES["logo"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
For further information
My script for uploading images is at
C:/wamp64/www/store/settingstore.php
And the location I want to upload photos are
C:/wamp64/www/uploads/store/logo

Does input type=files appear in $_POST?

I have this within a form and I was thinking that as its an input name I should be able to detect it with $_POST[] but I cant see it. Which would explain when I do isset on it nothing happens. Am I not understanding it correctly?
<input type="file" id="files" name="files" class="hidden" multiple="" >
<label for="files">Select file</label>
You can access posted file data in $_FILES
You can get file name, file type, tmp_name, error, size in $_FILES
Simple exmaple is:
Html:
<form action="upload_manager.php" method="post" enctype="multipart/form-data">
<h2>Upload File</h2>
<label for="fileSelect">Filename:</label>
<input type="file" name="photo" id="fileSelect"><br>
<input type="submit" name="submit" value="Upload">
</form>
In php:
<?php
if($_FILES["photo"]["error"] > 0){
echo "Error: " . $_FILES["photo"]["error"] . "<br>";
} else{
echo "File Name: " . $_FILES["photo"]["name"] . "<br>";
echo "File Type: " . $_FILES["photo"]["type"] . "<br>";
echo "File Size: " . ($_FILES["photo"]["size"] / 1024) . " KB<br>";
echo "Stored in: " . $_FILES["photo"]["tmp_name"];
}
?>
Files are stored in $_FILES, not $_POST
$_FILES variable
Manual on PHP File Uploads.
html form:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
upload.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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;
}
}
?>

Upload multiple images in php

i am creating a page called events i have created a form which submits on post file, this is my form code, `
Adding Staff
<div class="box">
<label>Heading</label>
<input type="text" name="heading" id="heading"/>
</div>
<div class="box" style="margin-left: 120px">
<label>Description 1</label>
<textarea cols="30" rows="5" name="description_1" id="description_1"></textarea>
</div>
<div class="box">
<label>Description 2</label>
<textarea cols="30" rows="5" name="description_2" id="description_2"></textarea>
</div>
<div class="clear"> </div>
<div class="box">
<label>Upload Images</label>
<input type="file" name="picture" id="picture"/>
<label>Is Rename <input type="checkbox" name="is_rename"> </label>
</div>
<div class="clear"> </div>
<div class="box" style="width: 100px; margin-left: 350px;">
<input style="padding: 4px" type="submit" name="submit" value="submit">
</div>`
This is my post
<?php
ob_start();
session_start();
include_once "../../classes/addClasses.php";
$heading = $_POST['heading'];
$description_1 = $_POST['description_1'];
$description_2 = $_POST['description_2'];
$picture = $_POST['picture'];
$target_dir = "../../../uploads/";
$target_file = $target_dir . basename($_FILES["picture"]["name"]);
print_r($_FILES);
if( isset( $_POST['is_rename'] ) )
$file_name = time().'-'.basename($_FILES["picture"]["name"]);
else
$file_name = basename($_FILES["picture"]["name"]);
if(empty($_FILES['picture']['name'])){
echo "please select a file to upload";
?>
<script>window.location.href="http://localhost/learner-pack/admin/admin/add_events.php?error=empty"</script>
<?php
}
$target_file = $target_dir . $file_name;
if (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["picture"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
if (file_exists($target_file)) {
echo "Sorry, file '".$file_name."' already exists.";
}elseif (move_uploaded_file($_FILES["picture"]["tmp_name"], $target_file)) {
echo "The file " . $file_name . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
/*$created_by = $_POST['created_by'];
$updated_by = $_POST['updated_by'];
$current_date = $_POST['current_date'];
$updated_date = $_POST['updated_date'];
$visitor_counter = $_POST['visitor_counter'];*/
if($admin->addingEvents($heading,$description_1,$description_2, $file_name))
{
header('Location: ../view_events.php?added=true');
}
else
{
echo "Error";
}
?>
please help me to store multiple images in table using one field and retireve them to view on events page when i fetch one row so that all images on that row are fetchable

Multiple files not uploading, not moving file in php

Im trying to upload multiple files but my code is bypassing the "move_uploaded_file" code. What is missing?
foreach ($_FILES['file']['name'] as $file) {
$target_dir = "uploads/";
$target_file = $target_dir . $file;
if (move_uploaded_file($file, $target_file)) {
echo "The file ".$_FILES["file"]["name"]. " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
move_uploaded_file needs to get the temporary file name: $_FILES['file']['tmp_name']
foreach ($_FILES as $file) {
$target_dir = "./";
$target_file = $target_dir . $file['name'];
if (move_uploaded_file($file['tmp_name'], $target_file)) {
echo "The file " . $file["name"] . " has been uploaded.<br />";
} else {
echo "Error uploading the file " . $file["name"] . ".<br />";
}
}
Small HTML snippet for the upload form:
<form action="./testpage.php" method="post" enctype="multipart/form-data">
<input name="file1" type="file" /><br />
<input name="file2" type="file" /><br />
<input type="submit" value="Upload!" />
</form>
Try this:
for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
{
if(!is_uploaded_($_FILES['file']['tmp_name'][$i]))
{
$messages[] = 'No uploaded';
}
else
{
if(#copy($_FILES['file']['tmp_name'][$i],$target.'/'.$_FILES['file']['name'][$i]))
{
$messages[] = $_FILES['file']['name'][$i].' uploaded';
}
else
$messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
}
}
}
html:
<form enctype="multipart/form-data" action="#" method="post">
<input id="uploadFile" name="file[]" type="file" />
<input id="uploadFile" name="file[]" type="file" />
<input type="submit" value="Upload" name="uploadt" />
</form>

Categories