uploading files in php using xampp - php

I have a problem in uploading files using php in XAMPP
my code is:
include 'header.html';
include 'header.php';
include 'debugging.php';
echo '<br />';
echo '<h1> Upload files</h>';
echo <<<_END
<br/>
<form method='post' action='FileUpload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='100' />
<input type='submit' value='Upload' />
<input type='hidden' name='submitted' value='1' />
</form>
<br/>
_END;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
if (isset($_POST['submitted'])) {
if ($_FILES && $_FILES['filename']['name']) {
$file_info = pathinfo($_FILES['filename']['name']);
$extension = $file_info['extension'];
include 'DO_Files.php';
$valid = DO_File::validExtension($extension);
echo '1';
if ($valid) {
echo '2';
$tmpName = $_FILES['filename']['tmp_name'];
$name = "Files//" . $_FILES['filename']['name'];
if (!move_uploaded_file($_FILES['filename']['tmp_name'], $name)) {
echo '3';
echo "<p>there was an error..</p>";
echo error_get_last();
} else {
echo '4';
$file = new DO_File();
$file->FileName = $name;
$file->FileSize = $_FILES['filename']['size'];
if ($file->save()) {
echo '5';
echo $file->FileName;
} else {
echo '6';
mysqli_error($file->dbc);
}
}
} else {
echo '7';
echo '<p>the file is not prompted</p>';}
} else {
echo '8';
echo 'no filee';
}
}
include 'footer.html';
I don't know why it doesn't upload. it works in remote server but with local server like XAMPP it shows an error on
if (!move_uploaded_file($_FILES['filename']['tmp_name'], $name)) {
any help? because I didn't figure put how to upload files to loacl server
I made a directory and the permissions are fine.

if (is_array($_FILES['filename']) && $_FILES['filename']['error'] == 0) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
$sourcePath = $_FILES['filename']['tmp_name'];
$targetPath = "Files//" . $_FILES['filename']['name'];
if (!is_dir('Files//')) {
mkdir('Files/', 0777, true);
}
if (move_uploaded_file($sourcePath, $targetPath)) {
echo $targetPath;
}
}
}

Related

Trying to save the picture onto another folder by adding random number In front of the picture

In my website there is, 6 digit random numbers known as refno Example 20221234and there is fileupload. Right now, it saves the photos with its own name on to the another filepath examplecat.png. Currently, I tried to add that refnoin front of the picture while saving like20221234cat.png. Is that possible to do?
<?php
$refno = isset ($_GET['refno'])? $_GET['refno']:'';
$file = isset($_FILES["file"]["tmp_name"])? $_FILES["file"]["tmp_name"] : "";
$file_size = isset($_FILES["file"]["size"])? $_FILES["file"]["size"] : "";
$file_name = isset($_FILES["file"]["name"])? $_FILES["file"]["name"] : "";
if(isset($_POST['submit']))
{
$dataDir = "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/";
if ($file_size <= 0)
{
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo " alert('No picture attached!')";
//echo $refno;
echo "</script>";
}
else
{
if(stristr($file_name, ".png")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
else if(stristr($file_name, ".jpg")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
else if(stristr($file_name, ".jpeg")){
$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd) or die ("Unableeeee to connect!");
$dest = $dataDir.$file_name;
if(move_uploaded_file($file,$dest))
{
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo " alert('Visual Defect Report and pictures are successfully submitted!')";
//echo $refno;
echo "</script>";
}
}
}
}
$file_name= "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/" . $refno.$file_name;
flush();
mysqli_close($conn);
?>
Below is how I get the refno.
<script type="text/javascript">
const now = new Date();
let randomNum = '';
randomNum += Math.round(Math.random() * 9);
randomNum += Math.round(Math.random() * 9);
randomNum += now.getTime().toString().slice(-2);
window.onload = function () {
document.getElementById("refno").value = `${new Date().getFullYear()}${randomNum}`;
}
</script>
<label class="control-label col-sm-4" for="refno">REF nos :</label>
<div class="col-sm-4">
<p class="form-control-static" style="margin-top: -6px;">
<input type="text" class="form-control" id="refno" name="refno" value="<?php echo $refno;?>" disabled>
</p>
</div>
<?php
if(isset($_POST['submit']))
{
$refno = isset ($_GET['refno'])? $_GET['refno']:'';
$file_tmp = isset($_FILES["file"]["tmp_name"])? $_FILES["file"]["tmp_name"] : "";
$file_size = isset($_FILES["file"]["size"])? $_FILES["file"]["size"] : "";
$file_name = isset($_FILES["file"]["name"])? $_FILES["file"]["name"] : "";
//Images Directory
$dataDir = "//sgewsnant21.amk.st.com/ews/web/webspool/temp/visualdefectreport/";
//New Filename
$new_filename = $dataDir.$refno.$file_name;
//File formats
$file_formats=array("jpeg","jpg","png");
//echo $file_size;
if($file_size>0){
//echo "Good";
$file_info = pathinfo($file_name);
$file_ext= $file_info['extension'];
//check the extension
if(in_array($file_ext,$file_formats))
{
//Database Connection
//$connection = mysqli_connect($apews_db_apews2, $apews_db_usr, $apews_db_pwd, $database) or die ("Unableeeee to connect!");
if(move_uploaded_file($file_tmp, $new_filename))
{
$message="alert('Visual Defect Report and pictures are successfully submitted!')";
}
else
{
$message= " alert('Error Uploading File!')";
}
}
else
{
$message= " alert('Invalid Picture Format!')";
}
}
else
{
$message= " alert('No picture attached!')";
}
echo "<script language=\"javascript\" type=\"text/javascript\">";
echo $message;
//echo $refno;
echo "</script>";
}
?>
<?php
if(isset($_POST['saveimage']))
{
$directory="images/";
$filename =$_FILES['file']['name'];
$filename_temp =$_FILES['file']['tmp_name'];
$new_filename = $directory.date("YmdHis")."_".$filename;
if(move_uploaded_file($filename_temp,$new_filename))
{
echo "File Uploaded ";
}
else
{
echo "File Upload Error.";
}
}
echo "<hr/>";
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="saveimage" value="save" />
</form>

PHP File upload, image not posted

I'm trying to create a form to upload a file, the problem is that the file won't be uploaded. in my code it returns "Image not uploaded".
I've searched a lot online and all the examples uses the same code.
Code:
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("/images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = '/images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype=”multipart/form-data”>
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>
The form has also other fields and the data are correctly send to the server.
Try this
<?php
if (isset($_FILES['image_url']) && is_uploaded_file($_FILES['image_url']['tmp_name'])) {
$is_img = getimagesize($_FILES['image_url']['tmp_name']); //Is an image?
if (!$is_img) {
$userfile_name = "It isn't an image";
}
else {
if (!file_exists("images/products/" . $_FILES['image_url']['name'])) {
$uploaddir = 'images/products/';
$userfile_tmp = $_FILES['image_url']['tmp_name'];
$userfile_name = $_FILES['image_url']['name'];
move_uploaded_file($userfile_tmp, $uploaddir . $userfile_name);
}
else {
$userfile_name = $_FILES['image_url']['name'];
}
}
}
else {
$userfile_name = "Image not uploaded";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?> " enctype="multipart/form-data">
<p><label for="image">Immagine: </label>
<input type="file" name="image_url"/></p>
<p><input type="submit" value="Salva" /></p>
</form>

Cannot interact with uploaded images from my website

When uploading images from my website into the designated folder I can see the file in the directory, but cannot open the files nor display them on the webpage.
EDIT This is an issue with my permissions, when trying to open the file in various programs I am receiving permission denied errors.
include('header.php');
$message = "";
$user_id=$_SESSION['user']['user_id'];
$images = getImageCount($user_id);
if(!isset($_SESSION['user']))
{
$_SESSION['message'] = "You must be logged in to manage your images";
header("Location:login.php");
}else if($_SESSION['user']['type'] == INCOMPLETE_USER)
{
$_SESSION['message'] = "You must create a profile to upload images";
header("Location:create_profile.php");
}else if($_SESSION['user']['type'] == DISABLED_CLIENT)
{
$_SESSION['message'] = "Your profile has been disabled";
header("Location:login.php");
}else if($_SERVER['REQUEST_METHOD'] == 'POST')
{
print_r($_FILES);
$user_folder="./profiles/". $user_id;
echo "test";
$file=$_FILES['uploadfile'];
//go to the profile table an SELEECT images FROM profiles WHERE user_id =
if ($images <= MAXIMUM_IMAGES)
{
if ($file['error']!=0)
{
$_SESSION['message']= "Upload Failed!";
}
else if ($_FILES['uploadfile']['type'] != "image/pjpeg" && $_FILES['uploadfile']['type'] != "image/jpeg")
{
$message = "Error! image file must be a'". DEFAULT_FILE_TYPE."'";
}
else if ($file['size'] > MAX_FILE_SIZE)
{
$message = "Error! File must be smaller than '".MAX_FILE_SIZE."' bytes";
}
else
{
$directory = "./profiles/".$user_id;
echo $directory;
//echo $user_folder;
if (!is_dir("profiles/".$user_id))
{
mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
echo 2;
}
$temp_name=$file["tmp_name"];
$new_count = $images + 1;
$file_name=$user_id."_".$new_count;
echo $file_name;
$full_file_name ="profiles/".$user_id."/".$file_name. ".jpg";
move_uploaded_file($temp_name ,$full_file_name);
pg_execute($conn,"update_images",array ($new_count,$_SESSION['user']['user_id']));
}
}
else
{
$message = "Error! no more than " .MAXIMUM_IMAGES . "picture can be uploaded";
}
}
else if (!empty($_POST['submit_changes']))
{
echo "Fail";
$images= $_SESSION['profile']['images'];
}
?>
<form id="uploadform" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php echo $message; ?>
<strong>Select image for upload: </strong>
<input name="uploadfile" type="file" id="uploadfile" />
<input type="submit" value="Upload New Image" />
<img src="profiles/sault/saultl_4.jpg" alt = "Sault"/>
</form>
<?php
include('footer.php');
?>
I think you did not set the right permission to the folder, just try it in this way:
if (!is_dir("profiles/".$user_id))
{
mkdir("profiles/".$user_id, intval( 0777, 8 ), true);
}
If this do not work we would need some more detailed information in order to help you!

Upload PDF Files PHP

I am trying to upload only PDF files in my form, but nothing happens. It doesn't save the file name in the database and the file does not save into the directory.
My PHP code is
if (isset($_POST['submit'])) {
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) {
if ($_FILES['healthfile']['type'] != "application/pdf") {
echo "<p>Class notes must be uploaded in PDF format.</p>";
} else {
$filesql = "INSERT INTO tbl_health (link) VALUES ('{$filename}')".die(mysql_error());
$fileresult = mysql_query($filesql, $con).die(mysql_error());
}
if ($fileresult) {
echo 'Success';
} else {
echo 'fail';
}
}
}
my form is
<form action="allhealth.php" method="post" enctype="multipart/form-data">
<label>Upload Your Health Certificate</label>
<span class="btn btn-default btn-file">
Browse <input name="healthfile" type="file">
</span>
<br/><br/>
<button type="button" name="submit" class="btn-success">Submit</button>
</form>
Please help!!
Use this
if(isset($_POST["submit"]))
{
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
$FileType = pathinfo($newname,PATHINFO_EXTENSION);
if($FileType == "pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{
$filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
$fileresult = mysql_query($filesql);
if (isset($fileresult))
{
echo 'File Uploaded';
} else
{
echo 'Something went Wrong';
}
}
else
{
echo "<p>Upload Failed.</p>";
}
}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}
}
Note Before upload file you should check whether its in correct format
and MySQL extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Use this it's working now I have tested at local.
<?php
if (isset($_POST['submit'])) {
$folder_path = 'health/';
$filename = basename($_FILES['healthfile']['name']);
$newname = $folder_path . $filename;
if ($_FILES['healthfile']['type'] == "application/pdf")
{
if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
{
$filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
$fileresult = mysql_query($filesql);
}
else
{
echo "<p>Upload Failed.</p>";
}
if (isset($fileresult))
{
echo 'Success';
} else
{
echo 'fail';
}
}
else
{
echo "<p>Class notes must be uploaded in PDF format.</p>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label>Upload Your Health Certificate</label>
<span class="btn btn-default btn-file">
Browse <input name="healthfile" type="file">
</span>
<br/><br/>
<input type="submit" name="submit" class="btn-success" value="submit">
</form>

Multiple File Upload PHP

I use the Following code to upload a single file .With This code I can Upload a single file to the database .I want to make multiple files uploaded by selecting multiple files in a single input type file.What Changes should i make in the code to make it upload multiple files?
<?PHP
INCLUDE ("DB_Config.php");
$id=$_POST['id'];
$fileTypes = array('txt','doc','docx','ppt','pptx','pdf');
$fileParts = pathinfo($_FILES['uploaded_file']['name']);
if(in_array($fileParts['extension'],$fileTypes))
{
$filename = $_FILES["uploaded_file"]["name"];
$location = "E:\\test_TrainingMaterial/";
$file_size = $_FILES["uploaded_file"]["size"];
$path = $location . basename( $_FILES['uploaded_file']['name']);
if(file_exists($path))
{
echo "File Already Exists.<br/>";
echo "Please Rename and Try Again";
}
else
{
if($file_size < 209715200)
{
$move = move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
$result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."')");
if ($result)
{
do {
if ($temp_resource = $mysqli->use_result())
{
while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
array_push($rows, $row);
}
$temp_resource->free_result();
}
} while ($mysqli->next_result());
}
if($move)
{
echo "Successfully Uploaded";
}
else
{
echo "File not Moved";
}
}
else
{
echo "File Size Exceeded";
}
}
}
else
{
echo " Invalid File Type";
}
?>
The Html That is used is
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file" id="uploaded_file" style="color:black" /><br/>
</form>
Basically you need to add to input name [] brackets and attribute "multiple"
<form id = "upload_form" method="post" enctype="multipart/form-data" >
<input type="file" name="uploaded_file[]" multiple="true" id="uploaded_file" style="color:black" /><br/>
</form>
Now all uploaded file will be available via
$_FILES['uploaded_file']['name'][0]
$_FILES['uploaded_file']['name'][1]
and so on
More info at
http://www.php.net/manual/en/features.file-upload.multiple.php

Categories