Do you see what's wrong here? I can't find anything.
this prints:
echo $_FILES["new_text_file"]["name"];
and this too:
echo $_FILES["new_text_file"]["tmp_name"];
php:
";
echo $_FILES["new_text_file"]["tmp_name"];
//Uploads the text file to the server
if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"]), $_FILES["new_text_file"]["name"])
{
//header('Location: ga-dev-exercise-pavan.php');
echo 'worked';
}else {
echo 'did not work';
}
}
?>
html:
<form enctype="multipart/form-data" action="ga-dev-exercise-pavan.php" method="POST">
Choose a text file you want to upload and search through:
<input type='file' name='new_text_file'>
<input type="hidden" name="submit_yes_file" value="true" />
<br /><br />
<input type="submit" value="upload">
</form>
You aren't automatically allowed to move the uploaded file everywhere. You need to move it somewhere your PHP script has permissions to write.
So e.g.
$src = $_FILES["new_text_file"]["tmp_name"];
$dst = './tmp_dir/'.basename($_FILES["new_text_file"]["name"]);
if (move_uploaded_file($src, $dst))
{
The basename protects you against the user specifying malicious file paths (does nothing against duplicate file names), the tmp_dir is a directory you can write to.
Using $src and $dst clears a bit the code, and allowed me to see that you had an extra parenthesis in your sample...
you should specify a target path
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
http://www.tizag.com/phpT/fileupload.php
Edit :
as mentioned in comments , parentheses are misplaced :
wrong :
if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"]), $_FILES["new_text_file"]["name"])
correct
if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"], $_FILES["new_text_file"]["name"]))
The parentheses were off. It should be like this:
if(move_uploaded_file($_FILES["new_text_file"]["tmp_name"], $_FILES["new_text_file"]["name"]))
Related
so what I am trying to do is utilize the backtick operator in php to run some python code that will display on my Apache Localhost. This python code contains the ability to edit arguments, but when I tried to edit the arguments, nothing came out on my localhost website.
My code:
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
<?PHP
$python = `python classify_image.py --image_file C:\xampp\htdocs\uploads\bamboo.jpg`;
echo $python;
?>
This is what I see in Localhost:
But when I remove the " --image_file C:\xampp\htdocs\uploads\bamboo.jpg" from the backticks, the python code reverts to reading the default image and returns this on my localhost:
Can someone tell me what is wrong with my use of the backtick operator in php?
You can use a variable inside the backticks.
You should also put the classify_image.py call inside the if, so you only do it when the file has been successfully uploaded.
And use forward slashes in the pathname rather than backslashes, or double the backslashes. In double quotes and backticks, backslash is an escape prefix, resulting in incorrect parsing of the pathname.
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "uploads/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
$python = `python classify_image.py --image_file C:/xampp/htdocs/$path`;
echo $python;
} else{
echo "There was an error uploading the file, please try again!";
}
}
?>
I have been back forward about this, can't find a solution. My file that I am trying to upload is not moving. I'm using WAMP and my root folder is C:\wamp\www
I've checked that the directory exists in php and put in a script that if it does not exist it should be created, which works, but still it is not moving the file. What am I doing wrong?
The Form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Upload.php
$target_path = "uploads/" ;
if(!file_exists($target_path)) {
mkdir($target_path, 0755, true);
echo "The directory was created";
}
$tmp_name = $_FILES['file']['tmp_name'];
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
echo $target_path;
if(move_uploaded_file($tmp_name, $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
try to change,
$tmp_name = $_FILES['file']['tmp_name'];
to
$tmp_name = $_FILES['uploadedfile']['tmp_name'];
I created an upload script using the resources from tizag, the script is returning an onscreen error however no error in the apache logs.
HTML Form
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000000" />
Select video to upload:
Please choose a file: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
PHP Code
<?php
$target_path = "/var/www/html/upload/";
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'] [0], $target_path))
{
echo "The file ". basename( $_FILES['uploadefile']['name'] [0]). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
?>
This shouldn't be very hard to impliment, but with no errors on apache it's very hard for me to troubleshoot. My php knowlegdge is limited so please bare that in mind.
Kind Regards,
Mark Couto
This line:
$target = $target_path . basename($_FILES['uploadedfile']['name'][0] );
As it stands, $target is just a stray variable and not being used anywhere else.
It should read as:
$target_path = $target_path . basename($_FILES['uploadedfile']['name'][0] );
"I created an upload script using the resources from tizag"
The Tizag tutorial you followed doesn't change their variables.
Their example:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
Source: http://www.tizag.com/phpT/fileupload.php
Plus, you have a typo in ['uploadefile'] which should read as ['uploadedfile']
First ensure that php is configured to allow file upload.
in your "php.ini" file search for the directive, and set it ON,
file_uploads= ON
Source : http://www.w3schools.com/php/php_file_upload.asp
I want a know easy method to upload a file to a target location that can be selected by user(Admin) before uploading a file . what is the dialog box or such selection method to use to select a particular target location in the server . There is a root directory .
In side the root directory there are folders called
2011,2012,2013,2014 .
In each of these directories there are some many directories so on .
So i want to manually select one directory and upload a file into that directory . I knew the php code for upload a file into a predefined directory . Now i want to change that code for my purpose .
This is the code that i used :
And this is the code example that i used :
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
And php code is
<?php
$ndir = $_GET['file'];
echo $ndir;
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
// Check if file already exists
if (file_exists($target_path)) {
echo "Sorry, file already exists.";
}
else if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
?>
Thanks
Since I had a few minutes to spare:
You can use a dropdown menu select while setting a name attribute with values set as folder names.
<select name="destination">
<option value="folder_1">Folder 1</option>
<option value="folder_2">Folder 2</option>
<option value="folder_3">Folder 3</option>
</select>
Then concatenate the folder destination to the upload/target path variable:
if(isset($_POST['destination'])){
$target_path = "uploads/";
$target_folder = $_POST['destination'];
$target_path = $target_path . $target_folder . "/" . basename( $_FILES['uploadedfile']['name'] );
}
Type of thing.
EDIT
This will scan your entire sub-folders starting from the starting path given.
Nota: You will need to fill in the rest for the uploading part.
<form enctype="multipart/form-data" action="" method="POST">
<select name="destination">
<?php
/* ============== PATH NOTES ============== */
// Either use full path. Keep the trailing slash
// $yourStartingPath = "/var/user/you/httpdocs/uploads/";
// or use current folder from script's execution
$yourStartingPath = "./";
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($yourStartingPath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);
foreach($iterator as $file) {
if($file->isDir()) {
echo '<option value="'.$file->getRealPath().'">' . $file->getFilename().'</option>';
}
}
echo "</select>";
if(isset($_POST['destination'])){
$target_folder = $_POST['destination'];
$target_path = $target_folder . "/" . basename( $_FILES['uploadedfile']['name'] );
}
echo "<br>";
/* You can comment out the echo $target_path; */
/* It's just for testing purposes to show you the folder */
echo $target_path;
echo "<br>";
?>
<input type="submit" value="Upload File" />
</form>
I have two files (html and php). The user uploads a file in the html form and the php file contains a script to upload the file onto a server.
It works perfectly, my problem is when User 1 uploads 'wordThing.docx' and User 2 comes along and uploads 'wordThing.docx', User 1's file will be overridden.
Here's my HTML code:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br />
<label for="yourName">Your name:</label>
<input type="textbox" name="yourName" id="yourName" /><br />
<input type="submit" name="submit" value="Submit"><br />
</form>
</body>
</html>
And this is my PHP script:
<?php
$userName=$_POST['yourName'];
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
?>
I would like to attach the text the user would enter into 'yourName'. Thus the server has contains different file names and none has to be overridden.
So assuming that the users have different names, I would like to know how do I save the file on the server with a name attached to it. For example: User 1's file upload would be 'wordThingSusan.docx'
I hope this isn't too much info. I just wanted to be clear and precise.
Just in case someone is trying to use this code, you need to have a folder named 'uploads' under your directory for it to work.
change
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
to
$target_path = $target_path . time() . rand(11, 99) . basename( $_FILES['uploadedfile']['name']);
The generated name will be something like uploads/123456789052wordThing.docx
time() will give you the time in format 1234567890, rand(11,99) will generate random number between 11 and 99, so even if 2 people upload same file at same time, the file won't be overwritted.
Just add a unix timestamp to the beginning of the filename:
<?php
$userName=$_POST['yourName'];
$target_path = "uploads/";
$target_path = $target_path . date('U') . '_' . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>
I would look into the file_exists method:
http://php.net/manual/en/function.file-exists.php
If the check came back that the file already exists I would rename the file and then save it with the new name.
That is how I would attempt it.