upload image on a parallel directory's folder - php

I have two directories:
/publichtml
/csp.example.com
My code is in csp.example.com/classes/ep-class.php and I want to upload my file on /publichtml/upload/photo folder. How can I achieve that also /publichtml is accessible using example.com so I am taking target directory as a absolute url?
Here is my code:
if(isset($_FILES["imagefile"]["type"][0]))
{
$filename=$_FILES["imagefile"]["name"][0];
// Storing source path of the file in a variable
$sourcePath = $_FILES['imagefile']['tmp_name'][0];
// Target path where file is to be stored
$targetPath = SITEURL."upload/photo/".$filename;
if(move_uploaded_file($sourcePath,$targetPath))
{
return "Yes";
}
else
{
echo $_FILES["imagefile"]["errors"][0];
die("File upload error !");
}
}
Any help would be highly appreciated.

Related

Warning : file_put_contents() failed to open stream : No such file or directory

I am trying to upload an image from android to mysql
Now the path can be successfully uploaded but there's something wrong on this code
<?php
require_once '../database/database.php';
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// base_64 encoded string from android
$imageData = $_POST['image'];
// edittext from android
// $imageName = $_POST['image_name'];
$path = "images/Sample.jpg";
$actualPath = "http://192.168.254.123/*****/admin/$path";
if($user->UploadFiles($actualPath))
{
file_put_contents($path, base64_decode($imageData));
echo "Success";
}
else
{
echo "Failed";
}
}
?>
On the line where file_put_content is the error . here's the full error
Warning file_put_contents(images/Sample.jpg):failed to open stream:No such file or directory in C:\xampp\htdocs\projectname\admin\apk_api\upload_profile.php on line 17
Here's the proof that I can actually save the path on my database
and here's my directory
Can someone please help me out.
The directory images/ does not exists from the point of view, where the admin\apk_api\upload_profile.php file is. The resulting directory path will be
C:\xampp\htdocs\projectname\admin\apk_api\images\
However, the target directory should be
C:\xampp\htdocs\projectname\admin\images\
Create a relative path based from the location of the admin\apk_api\upload_profile.php file. You have to use something like ../images/ to get to the target directory.

PHP File checking always returning false

This bit of code reads from an RSS feed and shoulfd detect if an image exisists on the server or not:
<?php
$feedURL = 'http://www.goapr.com/news/category/product-release/feed/';
$sxml = #simplexml_load_file($feedURL);
if($sxml){
$i=0;
foreach($sxml->channel as $channel){
foreach($channel->item as $item){
//if($i==6){break;}
if($item->prodimg=="~"){break;}
if($item->prodpage=="~"){break;}
$i += 1;
$file = 'http://www.goapr.co.uk'. $item->prodimg;
if (file_exists($file)) {
echo "The file $file exists";
} else {
echo "The file $file does not exist";
}
}
}
}else{
echo 'Sorry there was an error. The recent products will return shortly.';
}
?>
Many of the images do exist, but it returns all images not found:
The file http://www.goapr.co.uk/includes/img/newprod/ultras4.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/30tpulleys.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/tiguan.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/2325row.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/plus.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/2017r.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/v24gti.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/q5gen3.png does not existThe file http://www.goapr.co.uk/includes/img/newprod/s8downpipes.png does not existThe file http://www.goapr.co.uk/products/
All images are detected as not found.
Its not the same as: How to check if a file exists from a url as the files are on the same filesystem as this PHP page.
If you need to check if a file on the local filesystem exists you should refer to it via it's local path and not its URL, something like:
$file = $_SERVER["DOCUMENT_ROOT"]."/path/to/images/".$item->prodimg;
Note that the DOCUMENT_ROOT is the root path as set up by the webserver host (which means it may not work in the commandline.
You can also use __DIR__."/".$item->prodimg if the images are in the same directory as the currently running script.

error in creating directory using random function

I am trying to create a random directory for saving uploaded photos using the below code but its not working. Can anyone help.
//Photo upload script
if(isset($_FILES['profilepic']))
{
if(((#$_FILES["profilepic"]["type"]=="image/jpeg")||(#$_FILES["profilepic"]["type"]=="image/png")||(#$_FILES["profilepic"]["type"]=="image/gif")) && (#$_FILES["profilepic"]["size"]<2048576))
{
$chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM123456789";
$rand_dir_name=substr(str_shuffle($chars),0,15);
mkdir("./userdata/images/$rand_dir_name");
//mkdir("\\userdata\\images\\".$rand_dir_name,077,true); //tried this but no luck
if(file_exists("userdata/images/$rand_dir_name/".#$_FILES["profilepic"]["name"]))
{
echo #$_FILES["profilepic"]["name"]."Already exists";
}
else
{
move_uploaded_file(#$_FILES["profilepic"]["temp_name"],"userdata/images/$rand_dir_name/".$_FILES[profilepic][name]);
echo "Uploaded and Stored in userdata/images/$rand_dir_name/".#$_FILES["profilepic"]["name"];
}
}
else
{
echo "error";
}
}
mkdir("./userdata/images/$rand_dir_name");
This line should be either:
mkdir("../userdata/images/$rand_dir_name"); //in this format if you are using one directory back to the relative path
or
mkdir("/userdata/images/$rand_dir_name"); //in this format if you are trying to create from projects root path
or
mkdir("userdata/images/$rand_dir_name"); //in this format if you are trying to create a directory from the relative path
it cannot have single dot (.) followed by forward slash

deleting image from a file in php

I am trying to delete a file from folder in php here is my model function
function deleteFiles()
{
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
if(is_file($file))
{
#unlink($file); // delete file
echo $file.'file deleted';
}
else
{
echo "no file";
}
}
but I always see "no file" and file is never deleted, file is in folder,because the url in $file actually displays the file in browser
help me
instead of using web url
$file = "http://localhost/copycopy/img/uploaded/long.jpeg";
use local path to file:
$file = $pathToYourWebSite . "/copycopy/img/uploaded/long.jpeg";
be sure to set $pathToYourWebSite to real location of your website;

Upload image to existing folder PHP

<?php
include('includes/db.php');
$drinks_cat = $_POST['drinks_cat'];
$drinks_name = $_POST['drinks_name'];
$drinks_shot = $_POST['drinks_shot'];
$drinks_bottle = $_POST['drinks_bottle'];
$drinks_availability = 'AVAILABLE';
$msg = "ERROR: ";
$itemimageload="true";
$itemimage_size=$_FILES['image']['size'];
$iname = $_FILES['image']['name'];
if ($_FILES['image']['size']>250000){$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$itemimageload="false";}
if (!($_FILES['image']['type'] =="image/jpeg" OR $_FILES['image']['type'] =="image/gif" OR $_FILES['image']['type'] =="image/png"))
{$msg=$msg."Your uploaded file must be of JPG , PNG or GIF. Other file types are not allowed<BR>";
$itemimageload="false";}
$file_name=$_FILES['image']['name'];
$add="images"; // the path with the file name where the file will be stored
if($itemimageload=="true")
{
if (file_exists($add) && is_writable($add))
{
if(move_uploaded_file ($_FILES['image']['tmp_name'], $add."/".$_FILES['image']['name']))
{
echo "Image successfully updated!";
}
else
{
echo "Failed to upload file Contact Site admin to fix the problem";
}
}
else
{
echo 'Upload directory is not writable, or does not exist.';
}
}
else
{
echo $msg;
}
$dir = $add."/".$iname;
echo "<BR>";
// Connects to your Database
mysql_query("INSERT INTO `product_drinks`(`drinks_id`, `drinks_cat`, `drinks_name`, `drinks_shot`, `drinks_bottle`, `drinks_image`, `drinks_availability`) VALUES (NULL,'".$drinks_cat."', '".$drinks_name."','".$drinks_shot."','".$drinks_bottle."','".$dir."','".$drinks_availability."')") or die("insert error");
Print "Your table has been populated";
?>
The code I'm working on works but i have to create a new "image" folder for my admin folder. Is there any way that I could upload the file outside the admin folder and move it to to the original "image" folder". I know it's quite confusing but my directory looks like this.
clubmaru
-admin
-images
-css
-images
-js
You may be looking for PHP's rename function. http://php.net/manual/en/function.rename.php
Set the oldname parameter to the file (with its path) and the newname parameter to where you want it to be (along with the new path, obviously)
Just ensure the "image folder" you want to move the file to has the correct permissions set ensure it's writable. You also may want to consider changing the parameter in your move_uploaded_file to put the file where you want it in the first place!
Yes there is a way, you need to change the path. Right now you have the path as images/$name which means that it will put the file in the images directory found in the local directory to the script that is running.
Using directory layout:
clubmaru
->admin
->script.php (the upload file)
->images
->css
->images
->js
You make the path relative (or find another alternative)
$add="../css/images";
This means, go up a directory, go into css then into images.

Categories