Login|Contact Us
Question and Answer

Programming

Ask a Programming Question, Get an Answer ASAP!

  • Ask A Question
  • Browse Answers
  • Meet The Experts
  • How JustAnswer Works

i want to copy a file from a /pending/ directory to a /storage/

 
david's Avatar
  • Answered by:david
  • Programming Guru
  • Positive Feedback: 80.0 %
  • Accepted Answers: 83
Verified Expert
in Programming

Recent Feedback

Positive
I didn't follow-up on my end due to various factors. Not your fault, but not...
Positive
Excellent service, many thanks
Positive
Worked Great Thanks
Positive
Really helpful
Positive
Thanks for your help, the menus in Windows 7 were a little different to those...
Positive
great support - really tried to help me
Positive
Very patient and professional.
Positive
more knowledge about elder hardware would be good

Customer Question

i want to copy a file from a /pending/ directory to a /storage/ directory. the /pending/ file is called $string_final, the /storage/ file is called $paint

i am using a copy statement and then an unlink statement

problem: the syntax for using a variable name in the directory name doesn't work and it executes as soon as the page loads and not just after the 'submit' button is pressed

 

Optional Information:
Language (or Software): PHP

Already Tried:
if i hard code the file name the copy works but not with the variable, it must be a syntax problem i can't get it to only execute after the submit at all, i tried if(isset(submit)) but to no avail or if some field has content but it ignores it

Submitted: 296 days and 17 hours ago.
Category: Programming
Value: $45
Status: CLOSED
Picture
Expert:  Todd replied 296 days and 15 hours ago.

Hello, I can help you with this. Please upload the file you are using to http://wikisend.com at your convenience so that I can have a look.

Customer replied 296 days and 12 hours ago.

i uploaded index2.php to wikisend.com and it showed uploaded but went directly to HTTP 500 internal server error after that so i don't know if it took. i'm also not sure how you are going to see it as my file. please let me know. thx

Picture
Expert:  Todd replied 296 days and 11 hours ago.

If you did not see a download link appear then you probably encounted an error (that happens sometimes). Try using http://ge.tt

Customer replied 296 days and 7 hours ago.

ge.tt said it copied to http://www.ge.tt/46AGpJJ/v/0?c

let me know if thats enough information

Picture
Expert:  Todd replied 296 days and 6 hours ago.

I got it. I will take a look at it. If you happen to have a formatted copy that is more readable please upload that version instead.

Customer replied 296 days and 4 hours ago.

i don't understand what you are asking. what i uploaded should be what i'm working with. does it look weird or something???

Picture
Expert:  Todd replied 296 days and 3 hours ago.

The code I got was not indented so it is a bit harder to read given the size of the file so I was just wondering if you had a better formatted version. It's not a big deal if you don't, I will sort through what you sent.

Picture
Expert:  Todd replied 296 days and 1 hours ago.

Please find the updated file at http://ge.tt/61g0EKJ/v/0
It shoudl move the $string_final filename in the pending folder to a file named $paint in the storage folder. This will also work when the "Save Painting" button is clicked. Please let me know if it works for you as I can't test it and I will make any adjustments you need.

Remember, if for any reason you’re unsatisfied, we can continue our conversation until you’ve received the information you’re looking for. If I haven’t answered your question, please let me know and I’ll be happy to answer any other follow-up questions you might have.

Customer replied 295 days and 19 hours ago.

i uploaded the index2.php you modified and ran it several times, the rename function you created did nothing, no files were copied or deleted. now what???

 

i just tried adding an 'echo' statement inside the if statement you made. when i reloaded and ran the php it said hello so that indicates that is sees the statement as true

 

i also copied your 'rename' statement outside the if statement and put it inside an 'echo' line and it shows to the screen with both variables and directory structures in the right sequence

 

it really looks like it should work

 

i also checked all 3 relevelant directories on my site and there is no php_error.php file in any of them showing that no significant error was created by running the script in that directory

 

i don't know if you can tell but i keep editing this and adding comments and i think of things, lol

 

what about this??? i have selected the jpg file in the code above to show the name and display the image. did you try to execute the code? well...maybe i can't rename the file if its basically IN USE, it makes sense, if thats so, how can i release the file so the rename works?

 

just more food for thought

Picture
Expert:  Todd replied 295 days and 18 hours ago.

As I mentioned above I am not able to test this code since it is written for whatever application it is that you have setup along with the directory structure and so on.

You have confirmed that the correct values are present for $string_final and $paint, so that's a start. I don't see how the file could be in use unless there is something else taking place that I am not aware of. Does the script have access to write to the storage directory? You can temporarily set the permissions on storage to 777 to check if that's the issue. Also confirm that the path is correct for /home/davidsdo/... etc.

Customer replied 295 days and 17 hours ago.

ok, i did some more testing, i copied the code from the submit button down to the if statement with the rename inside to another php file.

i ran it and nothing happened AGAIN!!!

then i commented out the input button and removed $submit1 from the if statement

it worked YEAH!!!

so now the question is why doesn't the submit1 button not work?

below are the lines. i remarked out the first two and removed the $submit1 && from the 3rd one. what do you think is wrong???

<input type='submit' name='submit1' value='Save Painting' />

$submit1=$_POST['submit1'];

if ($submit1 && $paint == true) {

Picture
Expert:  Todd replied 295 days and 17 hours ago.

Hard to say without a look at the actual webpage that is generated but you can try the following:
if ($_POST && $paint == true) {
...

Customer replied 295 days and 13 hours ago.

the actual webpage is the index2.php i sent you. if you remove the references to opening the database and change the directory structure to what you have everything will work. let me know if this helps. there are no other files that interact with this one, just don't use the buttons on the top left. they are connected to other files but are not part of the problem at hand.

thanks

Picture
Expert:  Todd replied 295 days and 10 hours ago.

I could not reproduce it with exactly since I don't have all of those text files but after removing those files (categories.txt, years.txt, etc.) I was able to use the code as expected. Did you have a chance to check the permissions and file path as I mentioned before?

Customer replied 295 days and 5 hours ago.

i did test that and without the if submit1 in there the rename command works fine and moves the file from one directory to another, therefore, i don't see how it could be a permissions issue, it now doesn't work with the submit1 == true part of the if statement

Picture
Expert:  Todd replied 295 days and 4 hours ago.

I wasn't aware that you got it working to move the file, wasn't that the issue you were facing in your question?

Customer replied 294 days and 19 hours ago.

yes partly, i wanted to move the file and get it to only execute when the submit button was pressed. now we just have the submit button problem. if i take the $submit1 portion of the if statement out it executes when the php file loads, if i use the $submit1 in the if statement it doesn't work at all

my bad, i'm doing so much i can't keep up with what i'm saying

the rename works in a test file but not in the actual index2.php file and still without the $submit1 portion

i don't know about the submit button portion but i'm thinking that there is a in use issue with the index2.php file because the test.php file does not look at the file before doing the move. i am going to upload the test.php file that does work and will post the link for you to look at in a minute.

http://ge.tt/9mR8mOJ/v/0

i just tried inserting an IMG statement before the submit button hardcoding the Connection.jpg file. it doesn't display because it immediately copied the file and had nothing to display in the /pending/directory.

i inserted thhe $submit1 && into the if statement and it shows the picture but again will not execute the rename command.

 

something is definately wrong here. there must be something that we are missing. i am going to continue to google and see what i can come up with. i will post any findings here. you please do the same.

i just tried coding the rename command into the submit button command itself as an onClick and an onSubmit command. it still executes when the page loads. i don't see why it wouldn't wait until the button is clicked when its inside the button itself. who the he** knows.

HELP!!!!!!!

 

Picture
Expert:  Todd replied 294 days and 17 hours ago.

My test yesterday using the file you sent me worked using $submit1. It makes logical sense. When the form is submitted $_POST['submit1'] contains the value of the button, Save Painting. This evaluates to true for a !empty test. To demonstrate place:
var_dump(!empty($submit1)); before if ($painting...). Also do a print_r($_POST); to see if anything is passed for submit1 (as it could be conflicting with something else on the page). Everything should also be wrapped inside of a block for handling POST requests only as shown below:

There is also some inconsistency between in the variable naming regarding "$paint" and "$painting". Please be sure to update your code as shown below to either use the variable named $paint OR $painting. $painting is used below and should work.

if ($_POST) {
$submit1=$_POST['submit1'];
$filename=$_POST['painting'].".jpg";
$painting=$_POST['painting'];
$string_final="Asanti.jpg";
print_r($_POST); // check to see that submit1 is set in here
var_dump(!empty($submit1)); // another check for submit1
if ($submit1 && $painting) {
rename('/home/davidsdo/public_html/marknutt/photos/pending/' . $string_final,'/home/davidsdo/public_html/marknutt/photos/storage/' . $painting);
$inputarray="0";
}
}

You test.php file won't work for submit1 because the button is not in a form.

Customer replied 294 days and 16 hours ago.

i appreciate all your work on this issue. i have dealt with several other programmers and they could not fix the problem. i have not tried your last recommendations but will. i have to go to the hospital today and stay until tomorrow night. unfortunately they do not have internet so i can't work. i will resume testing on either friday night if i get back home early enough or saturday. please do not close this question until after i test it. thanks again.......david

Picture
Expert:  Todd replied 294 days and 16 hours ago.

I'm hoping it is the difference in the variable name, in the original code the variable was declared as $paint but the if statement had $painting, which would explain why it was not performing the operation. Anyway, I hope all goes well at the hospital. The question will remain on here till you get back.

Customer replied 293 days and 20 hours ago.

hi, i found somewhere to get online for awhile. we are really close. the print_r command helps. once the form is submitted once the next time its run the painting field is populated with the name from the first run. i tried a 'function prepopulate' above the input for the painting field, it loads but actually doesn't do anything, can you show me the syntax and placement of a 'function prepopulate'?

i have:

function prepopulate($painting) {
$painting=" ";
}

it is in the same form. like i said just above the input field for painting. i put:

value="<?php prepopulate('painting');?>"

am i missing something???

Picture
Expert:  Todd replied 293 days and 16 hours ago.

if you want to prepopulate the painting field with the last value you can do something like (updated line):
p, li { white-space: pre-wrap; }

echo "<center>"; echo "Painting Title:"; echo "<input type='text' name='painting' size='30' " . (isset($_POST['painting']) ? $_POST['painting'] : '') . " />"; echo "</center>";

What it does is check to see if a value is present from a previous submission and if it is assign it to the input's value, if not then nothing is displayed.


Customer replied 293 days and 6 hours ago.

hi,

i'm back home. but....i wanted just the opposite. i want to prepopulate the painting field with blank. i will also apply this to submit1. both of these retain information from previous submittions. this was detected from your idea of print_r.

Picture
Expert:  Todd replied 293 days and 6 hours ago.

With the code I provided, it will initially be blank then after submitting the form it will have the value that was entered by the user. That seems to be what you are describing.

Customer replied 292 days and 21 hours ago.

the "p, li { white-space: pre-wrap; } " causes a syntax error

Customer replied 292 days and 20 hours ago.

i'm a dodo head. of course that line goes in the head. got it

i'm close now, it places the right variables where they below. it still didn't work though so i echo'd the rename command and noticed that $string_final is blank when it tries to rename so the bool is false. i just don't know so i uploaded the file to http://ge.tt/3rE04VJ/v/0

of course i'll keep looking but if you would too i'd appreciate it.

do you ever have cases that last as long as this one?

its tiring

Picture
Expert:  Todd replied 292 days and 15 hours ago.

If $string_final is empty it would have something to do with $arr and/or $inputarray. Can you put print_r/var_dump statements to see what is in those variables to ensure they have the values that you expect?

Customer replied 289 days and 19 hours ago.

i'm getting tired of this, not you, just the problem. now it kinda works. it does the rename but....it actually deletes the whole /pending/ directory and actually creates a subdirectory under the /storage/ directory with the $filename name and puts the file desired under that directory.

it still appears from displaying the rename statement and the array that the $string_final is infact blank. what in the world.

i again have uploaded index2.php

http://www.ge.tt/19n6ebJ/v/0?c

HELP!!!

i would really luv to get this damn thing running today :)

Picture
Expert:  Todd replied 289 days and 16 hours ago.

A couple things: if $string_final is empty then the directory would be renamed (and not a file, since there would be no file specified). If $string_filnal is blank it depends on several other variables, I'm not sure if you saw this in my last message If $string_final is empty it would have something to do with $arr and/or $inputarray. Can you put print_r/var_dump statements to see what is in those variables to ensure they have the values that you expect?
$string/$string_final comes from $arr[$inputarray] so the source of the problem most likely has something to do with one of those variables. Please print them out to ensure they contain the correct values.

Customer replied 288 days and 17 hours ago.

i see what your saying, i do see that my variable is empty so it makes sense about the directories being created and deleted,

i have done a print_r/var_dump and yes it is empty, i see that

the $arr[$inputarray] works at the top of the file when it displays the content, i just don't know why is all of a sudden doesn't show at the portion where submit is hit

Picture
Expert:  Todd replied 288 days and 15 hours ago.

This is another issue which I still cannot test on my end given the way this is written. I will have to opt-out so that another expert can assist you with this.

Picture
Expert:  david replied 288 days and 12 hours ago.

I would be glad to finish this task for you.

To help me better understand the current status of your problem, could you describe the task you are trying to perform and which part is still not working?

Customer replied 288 days and 8 hours ago.

ok david,

i have two directories with photos, /pending/ /storage/

i have been trying to do a 'rename' after the submit button is pressed.

the /pending/ file is displayed but the filename for the /storage/ is user entered before hitting the submit button

this process has not worked completely at all, i originally opened this case on 6/2 and has been closed several time by other experts

different answers i have been given solves a portion but then causes another

currently i have the 'rename' working but the filename for the /pending/ directory is not inserting into the command so instead it deletes the /pending/ directory and creates another subdirectory under the /storage/ directory with the name of the destination input filename

http://ge.tt/19n6ebJ/v/0 has a copy of the index2.php file for your review.

please help

Picture
Expert:  david replied 288 days and 7 hours ago.

ok. so if I have an image file 'test.jpg' stored in the same directory of the php file, can you walk me through the steps you take on the PHP page to do this operation?

Customer replied 287 days and 19 hours ago.

the data 'jpg' files are never in the same directory as the php files. php files are under the root directory of the site. files are uploaded to the /pending/ directory and then displayed as an array on screen. the user has an input field to select the 'jpg' needed. they then enter on the field and the right side of the screen shows the jpg filename on the right with an image of the file. they then add a ton of variables associated to this file including a new filename for permanent storage. then they hit submit, the new named file is moved to /storage/ directory (in theory) and the variables for the file and its new name are all stored in a dbms.

almost everything works. the problem is that the rename command was executing before the submit button was pressed. i am now using isset to ensure the submit button is pressed and the new filename the user inputted has been typed.

if you notice the index2.php file i sent, there are two variables for the rename statement, the 2nd one for the destination filename works but the 1st one for the pending name is XXXXX XXXXX have tried a var_dump and it does show empty,

i'm obviously missing something but i think i'm close, can you weed all this out???

thanks

Picture
Expert:  david replied 287 days and 15 hours ago.

Ok when I open the PHP file I see all the items in the current directory as well as all sub-directories (not just pending) inside 'Array'. Is this the expected behavior?

Also could you upload any other files that are required for this to run? It appears I will need all of the TXT files in your 'variables' directory.

Customer replied 287 days and 14 hours ago.

hi david, i'm not sure why you closed my question. i have a business and can only check my laptop from time to time during the day.

the text files in the /variables/ directory is only used if you select the variables option on the menu. this option is not used in my problem so the files are not required. just so you know the txt files in question are user entered data into each file from another menu and these entries are used to display dropdown lists the user can select. it has no bearing on this issue and is not required for the website to function.

as to your original question. at the top of the php file there is a commented section for displaying the array. this is where the /pending/ directory contents are displayed. i know if this directory does not exist then all directories and files are listed. if it does than only the jpg files in the /pending/ are displayed in the array.

Picture
Expert:  david replied 287 days and 12 hours ago.

don't worry, your question may have timed out but it is still open.

the way your file is coded, it will fail and exit if all of those TXT are NOT provided. as a temporary fix i have commented out all requests to the files to prevent this, but this may interfer with your problem later on.

also I currently have a JPG in the pending directory but I am still seeing all directories files in the array variables. can you point out which line to uncomment to fix this?

Customer replied 287 days and 12 hours ago.

the directory is refferenced on line 95. this is where i change to the /pending/ directory and create the array sequence.

Picture
Expert:  david replied 287 days and 10 hours ago.

OK I have that part working now.

Could you describe an example to me that you have tried that doesn't work?

Please tell me exactly what you are clicking on and where and what you are typing into fields on the page as well.

This way I can recreate your steps on my copy and find the source of the problem.

Customer replied 287 days and 4 hours ago.

under file to process type any number cooresponding to anything on the list to the left. under filename type any unique name. press 'save painting'. this should copy the file from the /pending/ directory to the /storage/ directory with the input file name. thats all i want. but only when the submit button is pressed.

Picture
Expert:  david replied 286 days and 16 hours ago.

I found the problem. I commented out these lines and replaced with the line below them:

//$string="$arr[$inputarray]";
//$string_final=substr($string,2,30)."\n";
$string_final = $_POST['inputarray'];

Replace your index2.php with this one:

http://www.wikisend.com/download/477136/index2.php

Keep in mind I still have the variable TXT files checks commented out, so you will need to remove the lines:

/* START TXT FILES

and

END TXT FILES */

Customer replied 285 days and 19 hours ago.

i did as you suggested and no difference, i downloaded the index2.php, unremarked out the section you remarked out and uploaded to my server. it still doesn't see the $string_final and deletes the /pending/ directory and creates a directory with that new file name under the /storage/ directory??? i reprepped the pending directory, i will leave it and you can check it out at www.davidsdomaindesign.com/marknutt/index2.php but you can only execute it once and see the echo'd statement then the /pending/ directory will be gone and i'll have to reset it again, let me know.


 

Picture
Expert:  david replied 285 days and 17 hours ago.

This is the test i ran that worked as you instructed:

1. put a jpg image into pending folder
2. type name of jpg (including extension) into 'file to process' field
3. type name of new title into 'painting title' field
4. hit 'save painting' button
5. file is moved from pending folder to storage folder with new name

Is the the test you tried?

Customer replied 284 days and 21 hours ago.

i think i see the difference, the 'file to process' is the number associated to the array shown on the left side not a file name. typing this number and pressing enter shows the associated file name to the top of that number above the input field for 'painting title'.

Picture
Expert:  david replied 284 days and 15 hours ago.

I see, I misread your previous post. I will change it to use the number.

Picture
Expert:  david replied 284 days and 11 hours ago.

OK, the number identifier for 'File to Process' works now. I had to change how $string_final was stored around line 140:

$string="$arr[$inputarray]";
$string_final=substr($string,2);

http://www.wikisend.com/download/644098/index2.php

Customer replied 283 days and 23 hours ago.

awesome, IT WORKS....almost :( your right if you type file to process and type the painting name it movies the file, unfortunately, you need to be able to type file to process (and press enter), this is how the picture is displayed and the variables are entered (the ones you echo'd out), then pressing submit should then move the file and it will write a record to a database.


can you do that???


otherwise your right, it does work and i probably should have clarified.


sorry

Picture
Expert:  david replied 283 days and 16 hours ago.

Well the standard is for HTML forms to behave the same whether you use the ENTER key or the form SUBMIT button. It is possible to hijack this process, but I wouldn't recommend it.

Is there a single action that would be suitable for both the ENTER key and SUBMIT that might be acceptable to you?

Customer replied 283 days and 13 hours ago.

i think i understand what you are saying. pressing enter after typing the file to process activates the right side of the page and allows all the other entered data to be written to the database. the submit writes the dbms record and moves the file to /storage/. is there another way to seperate these two functions?

Picture
Expert:  david replied 283 days and 12 hours ago.

Well you could use two seperate forms each with their own submit button, but this would be difficult to do since they both redirect to the same page.

It might be possible to simply have the submit button behave differently depending on if file to process is provided or not.

But then this would be confusing to the user. Is this page only going to be used by you, or do you have an audience that is expected to use this?

Customer replied 282 days and 19 hours ago.

i am designing the page, others will be running it. think about this though, entering the 'file to process' does not need to go anywhere, it just pulls local information and displays it, its the submit the needs to run another php file. does that make a difference? can we segerate them

Picture
Expert:  david replied 282 days and 16 hours ago.

to my knowledge, both the ENTER key press and the Submit button are POSTing data to index2.php and not a separate PHP file.

But before we continue, has your original problem been solved?

I read through your original question and that part appears to be working now, can you double-check?

Customer replied 282 days and 7 hours ago.

technically it was resolved, i asked to have the copy/move statements work, that does, it just doesn't work at the right time, i.e. pressing submit not just pressing enter. therefore i will say yes. now how do i close this ticket with you and ensure i get you when i open the next one? i don't want to have to start all over again with someone else. hell no LOL


let me know

Accepted Answer

Picture
Expert:  david replied 281 days and 17 hours ago.

To close the question simply rate one of my answers with a 3 or higher rating.

To open a new question to me you can click on my name and on my profile page, and ask the question in the question box.

Expert TypeProgramming Guru
Category: Programming
Pos. Feedback: 80.0 %
Accepts: 83
Answered: 7/3/2012

Experience: 6+ years of programming expertise

Ask this Expert a Question >
 
Tweet

2 Programmers are Online Right Now

Ask Your Question Now
Programming Questions Date Submitted
C ProgrammingDecode LabYour assignment is to write a C 4/9/2013
Reference the following instructions to assist you when completing 4/8/2013
First you will present the pseudo code with all the modules 4/8/2013
NA-112 4/8/2013
The first programming project involves completing a program 4/8/2013
RA-201 4/7/2013
This is my final exam for my c programming class. It is due 4/7/2013
Program Description Your program will display (see the sample 4/6/2013
RA-211 4/3/2013
ra-614 4/2/2013
RSS
Next 10 >
Ask A Programmer
Type Your Programming Question Here...
characters left:

Top Programming Experts

See More Programmers

In The News

Nbc
Washington Post
New York Times
Cnn
Learn More

How It Works

  • Ask an Expert
  • Get a Professional Answer
  • Ask Followup Questions
  • 100% Satisfaction Guarantee
Learn More
close
Find Expert answers related to your question.
Sign up using email
We will never post anything without your permission.
Already have an account? Sign in

Ask a Programmer

Get a Professional Answer. 100% Satisfaction Guaranteed.
80 Programmers are Online Now
Type Your Programming Question Here...
characters left:
Disclaimer: Information in questions, answers, and other posts on this site ("Posts") comes from individual users, not JustAnswer; JustAnswer is not responsible for Posts. Posts are for general information, are not intended to substitute for informed professional advice (medical, legal, veterinary, financial, etc.), or to establish a professional-client relationship. The site and services are provided "as is" with no warranty or representations by JustAnswer regarding the qualifications of Experts. To see what credentials have been verified by a third-party service, please click on the "Verified" symbol in some Experts' profiles. JustAnswer is not intended or designed for EMERGENCY questions which should be directed immediately by telephone or in-person to qualified professionals.
Truste
Contact Us | Terms of Service | Privacy & Security | About Us
© 2003-2013 JustAnswer LLC