Solved

Copying files into a newly created folder

Posted on 2004-08-28
13
236 Views
Last Modified: 2006-11-17
I'm having a problem creating folders and then moving images that are uploaded from a page into that folder.  I keep getting this error: Warning: copy(./$MemberName/images/image.jpg): failed to open stream: No such file or directory in /home/bigboost/public_html/members/publishform2.php on line 101.  But I clearly open and create the folders in the first four lines of code.  I labeled line 101, the problem line.  Can anyone see what I'm doing wrong and how to fix it?  The folders are created just fine, it's just that the image won't go into them.

Thanks for looking,
Erica

Here is my code:
<?php
mkdir("./$MemberName/", 0777);
chmod("./$MemberName/", 0777 );
mkdir("./$MemberName/images/", 0777);
chmod("./$MemberName/images/", 0777 );
?>
<?php
if (file_exists("./$MemberName/index.php")){
print("A gallery for $MemberName already exists.<br>");
}
else {
$path_to_file = './$MemberName/images/';
$files = $_FILES['files'];

if (!ereg("/$", $path_to_file))
        $path_to_file = $path_to_file."/";
$i = 0;
$imgname = array();
foreach ($files['name'] as $key=>$name) {
  if ($files['size'][$key]) {
     // clean up file name
    $name = ereg_replace("[^a-z0-9._]", "",
      str_replace(" ", "_",
          str_replace("%20", "_", strtolower($name)
          )
       )
     );
     $location = $path_to_file.$name;
//         while (file_exists($location))
//        $location .= ".copy";
     copy($files['tmp_name'][$key],$location);  <----Line 101, problem line
     $imagesToDisplay[] = $location;
   unlink($files['tmp_name'][$key]);
     echo "\n
 Successfully uploaded file: $name.<br>";      
      $imgname[$i++] = $name;

  }
}
}
?>
0
Comment
Question by:Job4Suprmn
  • 7
  • 2
  • 2
  • +2
13 Comments
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 30 total points
ID: 11923391
Your foreach looks funny to me - to handle multiple uploads, I would expect you to be looping through $_FILES itself. there's a whole of stuff that leads on from that. I don't think I would expect $_FILES['files'] to be defined at all; see http://www.php.net/manual/en/features.file-upload.php

Or is it that you have assigned multiple upload fields to an HTML array? If so, I'm not sure why you'd bother when $_FILES does it for you.

Judging by the error message, that variable name should never appear in there, only its value, so I suspected your quoting, and sure enough:

$path_to_file = './$MemberName/images/';

should be:

$path_to_file = "./$MemberName/images/";
0
 

Author Comment

by:Job4Suprmn
ID: 11923520
That worked, but now when I try to call the images back up it doesn't work...I just get nothing.  Am I calling them right?

if (is_file("/members/$MemberName/images/${imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/${imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/${imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\">");
}
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11923567
No, you've got the quoting syntax wrong:

${imgname[0]}

should be

{$imgname[0]}
0
 

Author Comment

by:Job4Suprmn
ID: 11923637
I've narrowed it down to this statement:
if (is_file("$MemberName/images/{$imgname[0]}")) {

I'm assuming it's something to do with $MemberName.  The directory should be right
0
 
LVL 9

Expert Comment

by:techtonik
ID: 11923832
Where did you get this $MemberName ?
It seems, that you 've got this from your $_GEt or $_POST arrays. In that case add
    $MemberName = $_GET['MemberName'];
at the beginning of your file.
0
 

Author Comment

by:Job4Suprmn
ID: 11923864
$MemberName is submitted from a form.  The whole script is for a user submitting images and then them being posted on a newly created page named $MemberName.php.  $MemberName works throughout the whole script except for on the line:

if (is_file("$MemberName/images/{$imgname[0]}")) {

$MemberName is also used to create a folder where $MemberName.php is located.  Everything is created fine, I just can't get the part to work where if the image exists, output it.

if (is_file("$MemberName/images/{$imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/{$imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/{$imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\" width=\"70\" height=\"52\">");
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:laurenty
ID: 11924094
try to do

print("$MemberName/images/{$imgname[0]}");

right before the is_file to see that the string looks like
0
 
LVL 7

Expert Comment

by:aib_42
ID: 11924345
> That worked, but now when I try to call the images back up it doesn't work...

"call", hmm. Is your problem part (if is_file...) in a function other than the rest of the code, where $MemberName is defined? Trying to use variables from an outer scope without putting a "global $varname;" inside your function is one common, simple mistake.
0
 

Author Comment

by:Job4Suprmn
ID: 11925569
Ok, when I do the print("$MemberName/images/....); it prints out exactly like it's supposed to.  So I'm assuming that the if (is_file("MemberName/images/{$imgname[0]}")) is wrong because of the directory being wrong... What I mean is...

The form is at http://www.bigboosting.com/membergal.php
and is submitted to http://www.bigboosting.com/members/publishform2.php
A page is created at http://www.bigboosting.com/members/$MemberName/index.php
and images for that page go to http://www.bigboosting.com/members/$MemberName/images/

On http://www.bigboosting.com/members/$MemberName/index.php is where you'll find this portion of the code:

print("$MemberName/images/{$imgname[0]}");
if (is_file("$MemberName/images/{$imgname[0]}")) {
print("<a href=\"/members/$MemberName/images/{$imgname[0]}\" target=\"_blank\"><img border=\"1\" src=\"/members/$MemberName/images/{$imgname[0]}\" width=\"70\" height=\"52\"></a>");
}
else {
print("<img src=\"/members/images/blank.gif\" width=\"70\" height=\"52\">");
}

But when I try if (is_file("$MemberName/images/{$imgname[0]}")) { it goes right to the else statement because I assume it's not reading that the file is there.  I've also tried (is_file("members/$MemberName/images/{$imgname[0]}")) { and get the same result.  So I'm lost.  Any insight?

0
 

Author Comment

by:Job4Suprmn
ID: 11925577
0
 

Author Comment

by:Job4Suprmn
ID: 11925630
And aib 42, $MemberName is working everywhere, is_file just can't find a file at that location (or any variation of /members/$MemberName/images/ or /$MemberName/images/ or just /images/)
0
 
LVL 9

Assisted Solution

by:techtonik
techtonik earned 20 total points
ID: 11926355
Of course it can't find it at /images, because you need to call it with relative path. If your code is called from  http://www.bigboosting.com/members/$MemberName/index.php then relative path would be ./images

Just use:
if (is_file("./images/{$imgname[0]}")) {

It should work.
0
 

Author Comment

by:Job4Suprmn
ID: 11926378
Oh duh!  Thanks techtonik, that worked, can't believe I forgot that.

Appreciate everyone's help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now