Upload image to a real web server is not working - php

I'm making a simple PHP application which can upload an image to server.
I did it in my localhost, everything works well, but when I did it in real server. I mean web.com, it didn't work.
Here is my PHP script :
<?php
require "init.php";
$encoded_string=$_POST["encoded_string"];
$userfname=$_POST["userNameOf"];
$decoded_string=base64_decode($encoded_string);
$string= str_replace(' ','', $userfname);
$path="http://mybookshare.com/photos/" .$string. ".jpg";
file_put_contents($path,$decoded_string);
$query = "UPDATE user SET ProfPicture='$encoded_string' WHERE UserFN='$userfname' AND ProfPicture IS NULL";
$result = mysqli_query($conn, $query);
if($result) {
echo "success";
} else {
echo "failed";
}
mysqli_close($conn);
?>
The script is located in htdocs/app, and the upload file destination of the picture is at htdocs/photos
Any help please?

Problem :
You don't have photos directory as in your script directory.
Solution :
Try to move your photos directory from your sub directory to your script main directory where your PHP file is present and then you will have no errors.As you can see that the script says that your directory is not present so that's why it's failing to do the rest of process.
Heads Up :
I already said the same thing in my first comment but seems like you didn't give much attention to it.as you were mentioning the photos directory as to be present in your php script directory for your php to work it out with that and then you were not having the photos directory there so that's what was causing the error.!

Related

A way to change the ownership of Apache to order creating txt file

Could you please tell me how to change Apache ownership in Windows if you guys know, since I cannot create txt files using PHP without permission. According to my issue, I need to be able to authorise a file to be made.
What I am trying to do is create a script that records keystrokes in the Firefox extension section. This script will send the data to an Apache PHP file and store it in a text file. I would appreciate your response if you could.
<?php
session_start();
if (!isset($_POST['key'])) {
echo ("Didn't received any new KEY strokes Yet!");
exit(0);
}
//read and write = a+, If the file does not exist, attempt to create it
$file_log = fopen("key.txt","a+");
if (!isset($_SESSION['site']) || $_SESSION['site'] != $_POST['site']) {
$_SESSION['site'] = $_POST['site'];
fwrite($file_log, "| site : ".$_POST['site']." | ");
}
fwrite($file_log,$_POST['key']);
fclose($file_log);
echo("text saved successfully");
It looks like you are not defining a full path for the file.
Depending on where php is running just calling fopen("key.txt","a+") might default to the root directory.
When creating/modifying files you should specify the full path to the file
fopen("/var/www/mydir/example/path/key.txt","a+")

PHP QR generator doesn't work on webserver

I have a small code in PHP to create a QR code from an url and store it on a webserver to use later:
function generate($content, $format = "png")
{
$encoded = urlencode($content);
$url = "http://www.esponce.com/api/v3/generate?content=$encoded&format=$format";
return $url;
}
$qr = generate("http://www.mywebsite.com");
$data = file_get_contents($qr);
$saved = file_put_contents('qr/my_qr.png', $data);
When I execute the script on my local computer, the QR code is stored correctly in the /qr folder. When I upload the script to the webserver and execute it, the file my_qr.png is created and stored in the /qr folder, BUT the file is empty (0kb).
I thaught it was caused by the permissions of the /qr folder, but putting the permissions to 777 gives no difference. Does anyone can explain to me how I can solve this?
Other solutions to create and store QR codes are welcome to.
Many thanks.
Steven

File can be opened in FTP but not in HTTP after upload from site

I am writing a PHP script that enables a user to upload a picture and then displays it on their page. Everything works fine up until the part where they need to display it. I run the form and submit it and the picture shows up in the directory in my FTP. I can download that file from the FTP and view it on my computer. I can visit the FTP url of that image, login and see it fine.
When I go to the HTTP version of the exact same URL, I get a 404 error. I have checked the permissions on the folder and it's ok to read and write for a user. I even checked the permissions on the file itself after it's uploaded and it's fine. Here's my PHP code when uploading the file:
<?php
include('connect.php');
$user_id = $_SESSION['user_id'];
if($_POST['submit']){
//GET FILE ATTRIBUTES
$name = $_FILES['myfile']['name'];
$size = $_FILES['myfile']['size'];
$tmp_name = $_FILES['myfile']['tmp_name'];
if ($name){
//start upload process
$location = "pics/$name";
move_uploaded_file($tmp_name,$location);
$sql = "UPDATE tbl_name SET imagelocation='$location' WHERE user_id='$user_id'";
$query = $mysqli->query($sql);
header('location:profile.php');
}
else{
die("Please select a file! <a href='profile.php'>GO BACK</a>");
}
}
?>
Any idea what this could be? I haven't seen this problem before.
i think folder permission may not be right when you created the folder with mkdir(). hope setting right permission will solve the problem.
It works now. I deleted the folder and recreated the folder with the same name. The folder was initially created through mkdir in PHP so I'm sure that had something to do with it. If anybody has any insight into why the folder wouldn't work with mkdir, feel free to post here. Thanks!

How to get permission to use unlink()?

I make a site and it has this feature to upload a file and that file is uploaded to a server
Im just a newbie to php I download xampp and I run this site that i made in my local machine.
My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
question:
How can I got permission to use unlink();?
I only run this on my localmachine using xampp
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
To do this you must be using superuser account or be the same user that have uploaded the file.
You can go to the directory from your command line and check the permissions that are set to the file.
The easiest solution is to loggin as administrator/root and delete the file.
Here is another work around:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
Read this URL
How to use Unlink() function
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
Try to use this in the path:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';

PHP Script is not Properly Uploading Images

I'm working on a small, user-maintained online store, and am trying to allow my end user (the store administrator) to upload graphics for products. When I run this script, however, it doesn't actually store the image. I built this script from various tips here and a tutorial, and have gotten everything but the image upload portion to work.
// Set the image target directory here
$target = "itemImages/";
$target = $target . basename($_FILES["image"]["name"]);
// Variables get POSTed here - just tack new ones on at the end.
// Various POSTs omitted for brevity
$pic=($_FILES["image"]["name"]);
// Places the picture in the folder
if(move_uploaded_file($_FILES["image"]['tmp_name'], "itemImages/"))
{
echo "The file " . basename($_FILES['uploadedfile']["name"]) . " has been uploaded.<br />";
}else {
echo "There was an issue adding this item. Please try again.<br />";
}
// Writes variables to the database
mysql_query("INSERT INTO tbl_item (itemNAME,itemDESC,itemCOST,itemHCOL,itemHSIZ,itemIMG)
VALUES ('$itemName','$itemDesc','$itemCost','$hasColor','$hasSize','$pic')");
mysql_close($con);
?>
Any help, tips, advice, insight, etc. would be very much appreciated.
move_uploaded_files requires a filename as its target. It does not blindly move to a directory, so
move_uploaded_files($_FILES..., 'somedir/somefile.txt');
works, but
move_uploaded_file($_FILES..., 'somedir/');
will not.
Plus, note that your database operation is vulnerable to SQL injection attacks. You're blindly inserting the uploaded file's remote name (['name'] via $pic), and that name is fully under the remote user's control.
Make sure the itemImages folder has write permission by the user your web server (e.g. Apache) is running as (e.g. www-data)
make sure the .php file and the folder you are writing to have the same "owner". Or try setting permissions on the itemImages folder to 777 (This is not recommended, just a debug tactic)

Categories