Trying to relocate image to another folder

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Trying to relocate image to another folder

Post by dlee » 2023-02-04 06:51

The following code does not work:

Code: Select all

	function duck_season_2013_after_insert($data, $memberInfo, &$args) {
	
		$old_dir = '../images/';
		$new_dir = '../images/gallery/duck-season-2013/';		
		
		$img_name = $_FILES['photo']['name'];
		$img_name = str_replace(' ','_',$img_name);
		$old_file = $old_dir.$img_name;
		$new_file = $new_dir.$img_name;
		
		copy($old_file,$new_file);
		unlink($old_file);

		return TRUE;
	}
This is what appears when I echo the variables:

Code: Select all

$_FILES["photo"]["name"] - Appgini screen capture 1.jpg
$img_name - Appgini_screen_capture_1.jpg
$old_file - ../images/Appgini_screen_capture_1.jpg
$new_file - ../images/gallery/duck-season-2013/Appgini_screen_capture_1.jp
I have this code in a test page in the hooks folder, test.php, and it works. Anyone have any idea why the top code doesn't work?
TD

Code: Select all

	$old_dir = '../images/';
	$new_dir = '../images/gallery/duck-season-2013/';
	$img_name = 'modified_login_page.jpg';
	
	$img_name = $_FILES[0][0];
	$old_file = $old_dir.$img_name;
	$new_file = $new_dir.$img_name;

	copy($old_file,$new_file);
    	unlink($old_file);

pböttcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Trying to relocate image to another folder

Post by pböttcher » 2023-02-04 08:27

Hi,

without digging into it, I would assume that you navigate through the wrong directory structure.

the after_insert function is located in the hooks directory, but called within the root directory. So the used path should be relativ to the root directory.

If you put your test script in the hooks folder and call it directly there, the current directory is the hooks folder and it will work.

So try

Code: Select all

 	function duck_season_2013_after_insert($data, $memberInfo, &$args) {
	
		$dir=dirname(__FILE__);
		$old_dir = $dir.'/../images/';
		$new_dir = $dir.'/../images/gallery/duck-season-2013/';		
		
		$img_name = $_FILES['photo']['name'];
		$img_name = str_replace(' ','_',$img_name);
		$old_file = $old_dir.$img_name;
		$new_file = $new_dir.$img_name;
		
		copy($old_file,$new_file);
		unlink($old_file);

		return TRUE;
	}
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

dlee
Veteran Member
Posts: 137
Joined: 2020-04-14 00:21
Location: South Carolina, USA
Contact:

Re: Trying to relocate image to another folder

Post by dlee » 2023-02-04 17:45

That did it, thank you pböttcher!!! I never would have thought that a script, located in the hooks folder, would be run from the root but makes sense now.

TD

Post Reply