move uploaded files into a folder - php

I'm trying to move uploaded files into a folder, but i can't do it. Somebody see the error?
My html code has this line:
<form id="form" method="post" action="save_new_article.php" enctype="multipart/form-data">
And this is php code:
include("bd.php");
header('Content-type: text/html; charset=windows-1251');
$category = $_POST['category'];
$name = $_POST['name'];
$description = $_POST['full_description'];
$price = $_POST['price'];
$way = "../galery";
$file = $_FILES['photo']['tmp_name'];
$file_name = $_FILES['photo']['name'];
$file_name = ext($file_name);
$way .= "/".$file_name;
move_uploaded_file($file,$way);
If somebody can detect the error, inform me please

You need to change the last line to:
move_uploaded_file($file, $way);
If you look at the docs for move_uploaded_file, you will see that the first parameter is the place where the file is currently. Aka $_FILES['photo']['tmp_name'] which you have saved as $file.

Change the last line to:
move_uploaded_file($file, $way);

Related

How to add a word at the end of filename when uploading in php

I want to add a word or string at the end of every file I upload in in php script. For example, music.mp3 would be music(jarahub.xyz).mp3 after been uploaded. I tried achieving that with this line of code
$filename = $_FILES['myfile']['name']. '(Jarahub.xyz)';
But it ended up changing the file extension please can anyone help me with a more efficient code.
Try it please:
<?php
$myFile = $_FILES['myfile']['name'];
$randomString = '(asd.xyz)';
$currentExtension = pathinfo($myFile, PATHINFO_EXTENSION);
$newExtension = $randomString . '.' . $currentExtension;
$info = pathinfo($myFile);
$result = $info['filename'] . $newExtension;
echo $result;
?>

PHP:Uploaded File not move to folder (CLOSED)

Hello i have one problem to ask. The problem is when i'm trying to upload an image to certain folder usin php, the image is not move.The image information is insert into database but only the image is not move to destination folder. The folder is empty but no error is show. My file_upload in Php.ini is on but still the image is not move.
Below is my code:
<form action='try1.php' method='post' enctype='multipart/form-data'>
<table border='1'><tr>
<td><input type='file' name='file_img' /></td><td>
<input type='submit' name='btn_upload' value='upload'></td></tr>
</table>
</form>
<?php
require 'conf.php';
$link = mysqli_connect($h,$u,$p,$db);
if(isset($_POST['btn_upload']))
{
$filetmp = $_FILES["file_img"]["tmp_name"];
$filename = $_FILES["file_img"]["name"];
$filetype = $_FILES["file_img"]["type"];
$filepath = "upload/".$filename;
move_uploaded_file($filetmp,$filepath);
$query = "insert into try (image,type,path) values ('$filename','$filetype','$filepath')";
$result = mysqli_query($link,$query);
}
?>
I hope some one can help me to solve this problem,thank you.
(solved)I have solved the problem. its not the code but my pc that have problem.Thank you for helping.
At first delete your upload folder. After that
Please keep below code in your code.
$filepath = "upload/";
$filePathWithFileName = "upload/".$filename;
if (!file_exists($filepath)) {
mkdir($filepath, 0777);
}
move_uploaded_file($filetmp,$filePathWithFileName);
$filepath = "./upload/".$filename;

file upload in PHP is not working

I'm trying to upload a file to this directory:
with this code:
<?php
$target_dir = "../../assets/image/product/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$image = $_POST['fileToUpload'];
$add = $_POST['add'];
$merk = $_POST['merk'];
$category = $_POST['category'];
$color = $_POST['color'];
$size = $_POST['size'];
$price = $_POST['price'];
$stock = $_POST['stock'];
if(isset($add)){
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
mysql_query("INSERT INTO item VALUES('','$merk','$category','$color','$size','$price','$stock','$image')");
echo "<script>window.location='item.php';</script>";
}
?>
but the uploaded file is not appearing in target directory
Make sure you have add enctype="multipart/form-data" to your <form> element in your html file
Basically you have to find what error messages for.
add this line at the top of your code
error_reporting(E_ALL);
and this one, if it's your local (not live) server
ini_set('display_errors',1);
so, you will be able to see the errors on screen
Finally, for the files upload make sure that upload folder have full permission and you have to check $_FILES['fileToUpload']['error']) first. it it's not 0, refer to the manual page for the message.

How to upload excel file to php server from <input type="file">

I want to upload excel file using to php and wanted to perform some file operation in that excel file. I am not able to upload the file , If any one have some Idea please help , in the server side how to get the path from where the file has been uploaded?
$target_dir = 'uploads/'; is not working for me. and My file is in D: Please help.
PHP CODE:
<?php
if(isset($_POST['SubmitButton'])){ //check if form was submitted
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
require_once dirname(__FILE__) . '/Includes/Classes/PHPExcel/IOFactory.php';
$inputFileType = PHPExcel_IOFactory::identify($target_file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($target_file);
$i=2;
$val=array();
$count=0;
for($i=2;$i<34;$i++)
{
$val[$count++]=$objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
}
//echo'<pre>';print_r($val);
}
?>
HTML CODE:
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="filepath" id="filepath"/></td><td><input type="submit" name="SubmitButton"/>
</body>
You first need to upload the file before the read line:
$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);
// rest of your code...
now you should be able to continue working on the file.
before this, the file never have been in your uploads folder.
if(isset($_POST['SubmitButton'])){
try { //attached file formate
$statement = $db->prepare("SHOW TABLE STATUS LIKE 'table_name'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
$new_id = $row[10]; //10 fixed
$up_filename=$_FILES["filepath"]["name"];
$file_basename = substr($up_filename, 0, strripos($up_filename, '.')); // strip extention
$file_ext = substr($up_filename, strripos($up_filename, '.')); // strip name
$f2 = $new_id . $file_ext;
move_uploaded_file($_FILES["filepath"]["tmp_name"],"uploads/" . $f2);
// Client's info Insert MySQl
$statement = $db->prepare("INSERT INTO table_name (files) VALUES (?)");
$statement->execute(array($f2));
$success_message = "Excel file upload successfully!";
}
catch(Exception $e) {
$error_message = $e->getMessage();
}
}
Form code:
<form action="" method="post" enctype="multipart/form-data"> <input
type="file" name="filepath" id="filepath"/></td><td><input
type="submit" name="SubmitButton"/>
</form>
U did not write code to upload file.
move_uploaded_file()
You have to move your file from the temporary upload directory to the directory you want it in with the move_uploaded_file() function.
There are 2 arguments you need to put for the move_uploaded_file() function - the temporary file you just uploaded (ex. $_FILES["file"]["tmp_name"]), and then the directory you want to move it to... So it would end up looking something like this: move_uploaded_file($_FILES["file"]["tmp_name"], $path_of_new_file)

Adding Files After mkdir

I can't quite figure out how to add files from a sample directory that I have. I would appreciate some help. I have successfully created a new directory when ever a user inputs their username, but the contents are empty.
Edit: I've created the desired directory structure, just need to include/copy over the pages that I have in the sample template.
<?php
$fileName = "/associate/sample/";
$Name = $_POST['name'];
$thisdir = getcwd();
$folderPath = $thisdir . '/' . $Name;
mkdir($folderPath);
chmod($folderPath, 0777);
file_put_contents (realpath("$fileName"), associate/$Name);
?>
You were close, just not quite doing it right. Look this over, and compare with what you have to see what I've done differently
$folder = "/associate/";
$name = 'Joshua';
$thisdir = getcwd();
$folderPath = $thisdir . $folder . $name;
$filename = $name.'.file';
if(!file_exists($folderPath)){
mkdir($folderPath);
chmod($folderPath,0777);
}
$textToWrite = 'this is some text';
file_put_contents(realpath($folderPath).'/'.$filename, $textToWrite);

Categories