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
https://forums.phpfreaks.com/topic/229270-testing-str-replacement/
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.

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

 

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

 

 

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.

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!");
	}

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!");
	}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.