Jump to content

Testing str replacement


emldesign

Recommended Posts

Hello all,

 

I have a script uploading files on the server.

 

I would like to send a confirmation to user if the file had been uploaded well.

 


$targetFile =  str_replace('//','/',$targetPath) . $filename;

move_uploaded_file($tempFile,$targetFile);

if (str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile))
{
		mail($to, 'upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded");
	}else {
		mail($to, ' upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT uploaded");
	}

 

I presume I do something wrong because it doesn't work. The successfull email is sent each time.

 

Thank you very much for helping,

Eric

Link to comment
Share on other sites

With this line:

 

if (str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile))

 

you are determining whether the file has been uploaded - however this function seems irrelevant to the upload (it will return a value if it can fine '' in the document root). Can I suggest something like:

 

if (file_exists($targetFile))

 

instead - i.e. checking to see if that file is now on the server.

Link to comment
Share on other sites

hi thank you for helping. I did :

 

if (move_uploaded_file($tempFile,$targetFile)){

		str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded on our servers.");
	}else {
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT successfully uploaded on our servers. !!!!!");
	}

 

This time nothing works any more. Here is a link to the page:

http://www.emldesign.be/test/physphar/

 

Thank you

 

Link to comment
Share on other sites

Thank you AbraAadaver

 

move_uploaded_file($tempFile,$targetFile);

str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);

	if (file_exists($targetFile)){

		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded on our servers.");
	}else {
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT successfully uploaded on our servers!");
	}

 

There is still something wrong because the progress bar stop suddenly before 100%.

 

Thank you

 

 

Link to comment
Share on other sites

You really shouldn't worry about the str_replace, it's not doing anything any more. Also, using the file_exists function is pretty pointless here.

 

if (move_uploaded_file($tempFile,$targetFile)){

		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded on our servers.");
	}else {
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT successfully uploaded on our servers!");
	}

 

Give that a go and let us know your results.

Link to comment
Share on other sites

Hello !

 

BETTER: This time uploading works but I received the wrong mail :

 

Dear Mr/Mrs test3, the file entitled physphar_test3_iPhone_intro_1299001253.pdf has been NOT successfully uploaded on our servers!

 

Even if the file "physphar_test3_iPhone_intro_1299001253.pdf" is on the server actually....    :s

 

 


$targetFile =  str_replace('//','/',$targetPath) . $filename;

	move_uploaded_file($tempFile,$targetFile);

	str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);

	if (move_uploaded_file($tempFile,$targetFile)){

		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded on our servers.");
	}else {
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT successfully uploaded on our servers!");
	}

Link to comment
Share on other sites

Because you've already uploaded the file, the temp file will no longer be available (as you've moved it) - use the code below:

 

$targetFile =  str_replace('//','/',$targetPath) . $filename;

	if (move_uploaded_file($tempFile,$targetFile)){

		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been successfully uploaded on our servers.");
	}else {
		mail($to, 'Upload notification', "Dear Mr/Mrs $fixedname, the file entitled $filename has been NOT successfully uploaded on our servers!");
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.