I'm making a ecommerce website with a CMS where I can edit, add products etc, when I add a product and choose and image from my computer it does not upload to my folder I made for images "uploads".
Does anyone know the solution to this, it's probably in my code but I can't seem to figure it out.
If I manually add the image to the folder uploads and choose the image when I add the product the image shows.
Here is my code:
function add_product() {
if(isset($_POST['publish'])) {
$product_title = escape_string($_POST['product_title']);
$product_category_id = escape_string($_POST['product_category_id']);
$product_price = escape_string($_POST['product_price']);
$product_description = escape_string($_POST['product_description']);
$short_desc = escape_string($_POST['short_desc']);
$product_quantity = escape_string($_POST['product_quantity']);
$product_image = escape_string($_FILES['file']['name']);
$image_temp_location = escape_string($_FILES['file']['tmp_name']);
move_uploaded_file($image_temp_location, UPLOAD_DIRECTORY . DS . $product_image);
$query = query("INSERT INTO products(product_title, product_category_id, product_price, product_description, short_desc, product_quantity, product_image) VALUES('{$product_title}', '{$product_category_id}', '{$product_price}', '{$product_description}', '{$short_desc}', '{$product_quantity}', '{$product_image}')");
$last_id = last_id();
confirm($query);
set_message("New Product with id {$last_id} was Added");
redirect("index.php?products");
}
}
The UPLOAD_DIRECTORY is a path that I have configured in a file named config.php and it looks like this:
defined("UPLOAD_DIRECTORY") ? null : define("UPLOAD_DIRECTORY", __DIR__ . DS . "uploads");
Related
I am trying to delete image from server but unlink is showing error that is not a error at all. My code is given below:
private function delete_image($ad_id){
$this->load->helper('file');
$sql = "SELECT image1,image2,image3 from ads where AdId = ?";
$query = $this->db->query($sql,array($ad_id));
//for fetching result
$result = $query->result_array();
//to make a array of all images
$img = [$result[0]['image1'],$result[0]['image2'],$result[0]['image3']];
$i = 0;
while ($img[$i]!= "edubuylogo.png" && $i<3){
unlink('./uploads/'.$img[$i]);
$i++;
}
}
and the link to image is http://localhost/edubuy/uploads/IMG_20180120_210433.jpg
And image is perfectly loading.
The error is Message:
unlink(/uploads/IMG_20180120_210433.jpg): No such file or directory
It seems that your computer is confused, and you should specify an absolute path. In CodeIgniter you can use the FCPATH constant, so do like this:
unlink( FCPATH . 'uploads/' . $img[$i] );
That assumes your index.php file is inside edubuy. If it's not then:
unlink( FCPATH . 'edubuy/uploads/' . $img[$i] );
i was trying to add images into magento products programmatically but no luck.
i find the function addImageToMediaGallery can do that but its not working
$p = Mage::getModel('catalog/product')->load($id);
// save product
foreach ($imgs as $k => $img) {
$img_path = Mage::getBaseDir('media'). $img;
if( !$k ) {
$p->addImageToMediaGallery( $img_path , array('small_image', 'thumbnail', 'image'), false, false);
} else {
$p->addImageToMediaGallery( $img_path , array(), false, false);
}
$p->save();
}
what i know
product is loading successfully i try $p->getName() and i get the title
the paths are correct, images do live in /home/tronixco/public_html/caskyco/media/temp_images/dji_phantom_4_drone_4k_hd_camerea_3_axis_gimbal_5km_range_with_28_minutes_flight_time_manfrotto_bag_pack_4_.jpg
addImageToMediaGallery is doing some work as it copy the images to catalog/product/d/j...
in the admin panel product_edit there is nothing , like no image have been uploaded what so ever
im running an external script and not getting any logs in var folder neither any error in error_logs files
this whole code is running inside a function product_add_images( $id , $images )
magneto version is 1.9.2.4
<?php
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);
$importDir = Mage::getBaseDir('media') . DS . 'bulkimages/101/';
$productsData = array('Purple-Crown-Ring-MFAS.jpg','Purple-Crown-Ring-MFC.jpg','Purple-Crown-Ring-MSAS.jpg','Purple-Crown-Ring-MSF.jpg');
$productSKU = '111';
$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
foreach($productsData as $fileName){
$filePath = $importDir.$fileName;
if (file_exists($filePath)) {
$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
$ourProduct->save();
echo "done ";
} else {
echo $productSKU . " not done";
echo "<br>";
}
}
?>
I'm trying to upload an image on blog but it just doesn't do it. No error mesgs, just doesn't upload it. Checked permissions on the folder and they work when i place the image in the images folder but not when adding a post.
Below is the code im using.
Any help is greatly appreciated.
$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
$post_content = mysqli_real_escape_string($connection, $_POST['post_content']);
$post_tags = mysqli_real_escape_string($connection, $_POST['post_tags']);
$post_status = mysqli_real_escape_string($connection, $_POST['post_status']);
// $post_comment_count = 4; //THIS NEEDS TO BE DYNAMIC
move_uploaded_file($post_image_temp,"../images/".$post_image) ;
// if(!move_uploaded_file($post_image_temp,"../images/".$post_image)){
// echo 'Moved successfully:';
// }else{
// print_r(error_get_last());
// }
$query = "INSERT INTO posts(post_category_id, post_title, post_author, post_date, post_image, post_content, post_tags, post_status)";
$query .= "VALUES ({$post_category_id},'{$post_title}','{$post_author}',now(),'{$post_image}','{$post_content}','{$post_tags}','{$post_status}') ";
$create_post_query = mysqli_query($connection, $query);
confirm($create_post_query);
}
Replace
move_uploaded_file($post_image_temp,"../images/".$post_image) ;
with
move_uploaded_file(
$post_image_temp,
__DIR__ . "/../images/" . $post_image
);
Change these
move_uploaded_file("$post_image_temp,","../images/" . $post_image) ;
if(!move_uploaded_file($post_image_temp,"../images/".$post_image)){
to these
move_uploaded_file("$post_image_temp,", "images/" . $post_image) ;
if(move_uploaded_file($post_image_temp, "images/" . $post_image)){
Tested and works fine now!
you can use below code for acessing images folder
move_uploaded_file($post_image_temp,dirname(__FILE__)."/images/".$post_image) ;
but you should make sure images folder exists in same folder as the php file. If images folder is in an higher directory than your php file then use
dirname(__FILE__)."../images/"
so i can upload my photo from my Android app fine to /var/www/html/ProductPhotos but when i want to get the name of the Product and use that as the name of the new directory and image name then its not working. I create the new directory and /var/www/html/ProductPhotos with 777 permissions (even though its super bad) but for now its what i need. here's my PHP code:
<?php
$ProductAccountName = $_POST['ProductAccountName'];
$ProductName = $_POST['ProductName'];
$ProductImage = $_POST['EncodedImage'];
$NewDirectory = "/var/www/html/ProductPhotos/" . $ProductAccountName;
mkdir($NewDirectory, 0777, true);
//$DecodedProductImage = base64_decode("$ProductImage");
//$ProductName = $ProductName .".JPG";
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName, $ProductName . ".JPG", $DecodedProductImage);
?>
You're using a comma instead of a period. And you're missing a slash.
file_put_contents("/var/www/html/ProductPhotos/" . $ProductAccountName . "/" . $ProductName . ".JPG", $DecodedProductImage);`
See the file_put_contents docs.
You may want to be put into place some checks to make sure the user doesn't use relative paths(using ../ as part the ProductAccountName, for example). Just be careful of the user using this to do malicious things.
I can add an image to a product during creating using the following script. However, I'm having trouble loading a product and add additional image to the product media gallery. Do I have to delete the product media gallery and readd all the images at once? It's one image per row in a csv file.
public function addImage($product, $image)
{
$imagePath = $this->downloadImage($image);
$product->setMediaGallery(array('images' => array(), 'values' => array()));
if (is_file($imagePath)) {
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
}
}
Why are you using a script to add images? Adding images in magento can be easily done via the admin interface.
Just login to the admin panel, Catalog > Manage Products > Edit Product > Images (on left panel).
You add the additional images using the following script.
$importDir = Mage::getBaseDir('media') . DS;
// additional images
if ($import_product[29] != '') {
$addImages = explode(",", trim($import_product[29]));
foreach ($addImages as $additional_image) {
$image_directory = $dir .DS.'data'.DS. trim($additional_image);
if (file_exists($image_directory)) {
$product->addImageToMediaGallery($image_directory, null, false, false);
} else {
$image_directory = $dir . 'data' . DS . 'comingsoon.jpg';
$product->addImageToMediaGallery($image_directory, null, false, false);
}
}
echo 'Additional images for product ' . $product->getName() . ' ' . $product->getId() . ' imported successfully' . PHP_EOL;
}
The $import_product[29] is an array of images separated by commas. Please refer my tutorial as well which explains how to set default image as well.
http://www.pearlbells.co.uk/how-to-add-main-image-and-additional-image-to-the-products-pro-grammatically-magento/