1

I've searched this exchange AND Google for the problem I'm facing. No results. So I once again turn to you.

Here's the case: I have a great amount of file names in an array, including their respective paths. This array is dynamically generated.

I need to rename the files in this array.

I though I'd put the rename in a foreach to loop over each file.

foreach($files as $value) {

rename($value,"downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);

}

I get the following error from PHP:

Warning: rename() expects parameter 1 to be a valid path, array given in /home/users/whatever/whatever/test.php on line 58

And indeed when I do a var_dump on $value, I get the following:

array(1) { [0]=> string(95) "downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_assessment_of_qrm_implementation (1).pdf" }

The things is though, I can't figure out how to get $value from an array containing a string, to just the string.

I hope you guys can help me out!

A var_export($files):

array ( 0 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_assessment_of_qrm_implementation (1).pdf', ), 1 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_021_2_aide_memoire_on_gmp_gcp (1).pdf', ), 2 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_024_2_aide_memoires_on_biotech (2).pdf', ), 3 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_023_2_inspection_of_quality_control_laboratories (2).pdf', ), 4 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_009_3_aide_memoire_on_utilities (1).pdf', ), 5 => array ( ), 6 => array ( ), 7 => array ( ), 8 => array ( ), 9 => array ( ), 10 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_008_4_site_master_file_copy1.pdf', ), 11 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_011_3_recommendation_on_computerised_systems (5).pdf', ), 12 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_005_3_parametric_release.pdf', ), 13 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_010_4_guide_to_good_practices_for_the_preparation_of_medicinal_products_in_healthcare_establishments_copy1.pdf', ), 14 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_014_3_recommendation_on_isolators.pdf', ), 15 => array ( 0 => 'downloads/gmp-guidelines/pics/ps_w_01_2015_joint_pics_ema_concept_paper_on_the_revision_of_annex_1.pdf', ), 16 => array ( ), 17 => array ( ), 18 => array ( ), 19 => array ( ), 20 => array ( ), 21 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_005_3_pics_gmp_guide_for_blood_establishments.pdf', ), 22 => array ( 0 => 'downloads/gmp-guidelines/pics/pe_011_1_pics_gdp_guide_copy1 (1).pdf', ), 23 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_008_3_guide_to_plasma_inspections.pdf', ), 24 => array ( ), 25 => array ( 0 => 'downloads/gmp-guidelines/pics/ps_inf_20_2011_qa_distribution_activities_for_apis_may_2010.pdf', ), 26 => array ( ), 27 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_012_3_recommendation_on_sterility_testing.pdf', ), 28 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.doc', 1 => 'downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.pdf', ), 29 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_019_3_smf_source_plasma_establishments.doc', 1 => 'downloads/gmp-guidelines/pics/pi_019_3_smf_source_plasma_establishments.pdf', ), 30 => array ( ), 31 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_006_3_recommendation_on_validation_master_plan.pdf', ), 32 => array ( ), )

1 Answer 1

3

You have an array of arrays, so $value is an array. Access it accordingly:

foreach($files as $value) {
    rename($value[0],"downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);
}

Using the var_export() you provided, it looks like you also loop through the values (Which can also be an array). Notice this:

[28] => Array
    (
        [0] => downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.doc
        [1] => downloads/gmp-guidelines/pics/pi_020_3_smf_plasma_warehouses_copy1.pdf
    )

Array index 28 has 2 files in it, so we need 2 foreach loops. If the array is empty, the inner foreach loop will simply do nothing. (See this playground):

<?php
$files = array ( 
    0 => array (
        0 => 'downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_asse‌​ssment_of_qrm_implem‌​entation (1).pdf', 
    ),
    1 => array ( ),
    2 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_021_2_aide_memoire_on_gmp_‌​gcp (1).pdf', )
);

foreach( $files as $values ) {
    //You may have multiple values, so we need to loop through them.
    foreach ( $values as $value ) {
      rename($value, "downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title);
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I suck at arrays so I'm glad you are willing to help, PHP provides me with this: Notice: Undefined offset: 0 in /home/users/whatever/whatever/test.php on line 58 Line 58 being: rename($value[0],"downloads/gmp-guidelines/pics/gmp-guideline-pics-".$LFocus."-".$Title)
@JulianKoster This is the same exact code where you var_dump()'d $value?
Do var_export($files) right above the foreach loop, and post it here.
It's messy! array ( 0 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_038_1_aide_memoire_on_assessment_of_qrm_implementation (1).pdf', ), 1 => array ( ), 2 => array ( 0 => 'downloads/gmp-guidelines/pics/pi_021_2_aide_memoire_on_gmp_gcp (1).pdf', ), 3 => and so forth (the comment became too long)
@JulianKoster Edit your original question with the full var_export(). This array is a mess (The elements don't even line up the same).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.