Errors when I upload via copy files

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
btrade
User
Posts: 357

Errors when I upload via copy files

Post by btrade »

Hello

I have the errors when I upload files via copy a record.

D:\OpenServer\domains\app\office\models\ProductsAdd.php(1419): rename(D:\OpenServer\domains\files\temp__aptm2msps9n4o5lm83ior8lmicufeokv\products\x_product_img\-1/2023/03//6419d45868a14.jpg,D:\OpenServer\domains\files\temp__aptm2msps9n4o5lm83ior8lmicufeokv\products\x_product_img\6419d45868a14.jpg): The system cannot find the path specified (code: 3)


arbei
User
Posts: 9288

Post by arbei »

btrade wrote:

\-1/2023/03//6419d45868a14.jpg

Are you trying to use some dynamic upload path or file name?


btrade
User
Posts: 357

Post by btrade »

Yes, I use a custom function for rename files Before insert to DB.


arbei
User
Posts: 9288

Post by arbei »

You may post your custom function for discussion. And where did you use it?


btrade
User
Posts: 357

Post by btrade »

It's only when I copy records for adding.

Global code

function createDirectoryRecursive($dir) {
    if (!file_exists($dir)) {
        mkdir($dir, 0777, true);
        file_put_contents($dir . '/index.html', '');
    }
}
`
function processPhotos($filenames, $user_id, $dir) {
    $new_files = array();
    $current_date = date('Y/m/');
    $allowed_types = array('image/jpeg', 'image/png', 'image/gif');
    createDirectoryRecursive($dir . '/' . $user_id);
    createDirectoryRecursive($dir . '/' . $user_id . '/' . date('Y'));
    createDirectoryRecursive($dir . '/' . $user_id . '/' . date('Y/') . date('m'));
    $filenames = explode(",", $filenames);
    if (is_array($filenames)) { 
        foreach ($filenames as $filename) {
            $filepath = $dir . '/' . $filename;
            if (!file_exists($filepath)) {
                continue;  
            }
            $mime_type = mime_content_type($filepath);
            if (!in_array($mime_type, $allowed_types)) {
                continue; 
            }
            $extension = pathinfo($filename, PATHINFO_EXTENSION);
            $new_filename = uniqid() . '.' . $extension;
            $new_path = $dir . '/' . $user_id . '/' . $current_date . $new_filename;
            $new_db = $user_id . '/' . $current_date . $new_filename;
            if (copy($filepath, $new_path)) {
                unlink($filepath);
                $new_files[] = $new_db;
            }
       }
    }
    return implode(',', $new_files);
}

function Row_Inserted($rsold, &$rsnew)
 
    $filename = $rsnew['product_img'];
    $new_path = processPhotos($filename, CurrentUserID(), '../files/products');

    ExecuteStatement("UPDATE products SET product_img = '" .$new_path. "' WHERE id = " . $rsnew['id']);
}

mobhar
User
Posts: 11660

Post by mobhar »

Why didn't you use Row_Inserting server event instead?

Here is for your reference: How to rename multiple files and displayed it vertically (v2022). It should work either for v2023. Please CMIIW.


btrade
User
Posts: 357

Post by btrade »

I am using it, but it don't work correctly with dynamical directories.


mobhar
User
Posts: 11660

Post by mobhar »

Then you may post your code in that Row_Inserting server event for more discussion.


Post Reply