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!";
}
}
?>
Related
This issue might have been discussed multiple times but I wanted a simple PHP script to upload a file, without any separate action file and without any checks. Below is my written code:-
<html>
<head>
<title>PHP Test</title>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="upload file" name="submit">
</form>
</head>
<body>
<?php echo '<p>FILE UPLOAD</p><br>';
$tgt_dir = "uploads/";
$tgt_file = $tgt_dir.basename($_FILES['fileToUpload']['name']);
echo "<br>TARGET FILE= ".$tgt_file;
//$filename = $_FILES['fileToUpload']['name'];
echo "<br>FILE NAME FROM VARIABLE:- ".$_FILES["fileToUpload"]["name"];
if(isset($_POST['submit']))
{
if(file_exists("uploads/".$_FILES["fileToUpload"]["name"]))
{ echo "<br>file exists, try with another name"; }
else {
echo "<br>STARTING UPLOAD PROCESS<br>";
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tgt_file))
{ echo "<br>File UPLOADED:- ".$tgt_file; }
else { echo "<br>ERROR WHILE UPLOADING FILE<br>"; }
}
}
?>
</body>
</html>
I saved it in /var/www/html/phps/ location. But everytime I try to upload the file, I get ERROR WHILE UPLOADING FILE error. What am I doing wrong here. P.S. I have no previous experience of PHP, I just started with bits & pieces from internet.
Thanks
kriss
<?php
$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["fileToUpload"]["name"]);
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.";
}
?>
I hope that this thing will work and this is what you are in need of.
<?php
$name = $_POST['name'];
$image = $_FILES['fileToUpload']['name'];
$tempname = $_FILES['fileToUpload']['tmp_name'];
move_uploaded_file($tempname, "foldername/$image");?>
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'm trying to test a simple file upload script but it fails. Here is the html portion.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title="testing"></title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input type="file" name="uploaded"/>
<input type="submit" value="Upload File"/>
</form>
</body>
And below this is the php portion.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
It fails at this line:
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
And execute the else portion: "Sorry, there was a problem uploading your file." How can I get it to work without failing. Thank you for you help.
The most likely reason is lack of write access to the target directory.
Also, I'd check if $target resolves correctly, as there could be artifacts in the filename that seem to be passed directly to the script and are not filtered.
You could test the permissions with
is_writable( PATH_OF_FOLDER )
I also tend to test the existance of the file with
is_file( PATH_OF_NEW_FILE )
before confirming the upload is successful!
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"]))
I am getting the following errors:
Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 5
Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 7
Here is the code:
<?php
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']). " has been uploaded</p>";
} else{
echo "<p>There was an error uploading the image, please try again!</p>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
<fieldset>
<input name="theimage" type="file" />
<input type="submit" value="Upload" />
</fieldset>
</form>
</body>
</html>
Any ideas on how to fix this please ?
<?php
if(!empty($_FILES['theimage'])) {
// YOUR CURRENT PHP CODE HERE
?>
The problem is the upload script is executing even if nothing is posted (like when you first load the page).
Check to make sure you're getting data in $_FILES or $_POST before you try to do anything with them:
<?php
if(isset($_FILES)) { // if(isset($_POST))
// would work as well
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']).
" has been uploaded</p>";
} else {
echo "<p>There was an error uploading the image!</p>";
}
}
?>
Change your submit button to:
<input type = 'submit' name = 'theSubmitBtn' value = 'Upload'>
Then, wrap all of your php code at the top inside of this if check:
if(isset($_POST['theSubmitBtn'])) {
... Your Code ...
}
See if that works for you. On a side note, you will also need to add much more error checking if this is to be used in a production environment.